﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO.Ports;
using SerialPortHelperLib;
using System.Drawing.Drawing2D;
using System.Threading;
using XChipTool.Enties;
using System.Configuration;
using XChip_Common;
using BinAnalysisHelp;

namespace XChipTool
{
    public partial class XChip_Lite_AE : DevExpress.XtraEditors.XtraUserControl
    {

        public SerialPort sPortTemp;
        private bool isRead = false;
        private KeyEventArgs keyEnter = new KeyEventArgs(Keys.Enter);//用来手动触发文本框下发配置
        private long[] m_nFollowArea = new long[8];
        private bool isWrite = false;
        private addressregisterManager aBll;
        private List<addressregister> XChipAe;

        List<string> listFiltration = new List<string> { "AE_WIDTH", "AE_HEIGHT", "AE_WIN_WIDTH_X", "AE_WIN_HEIGHT_Y", "AE_WIN_WIDTH", "AE_WIN_HEIGHT" };
        //记录是否使用USB设备读写
        public bool IsUsbOpen = false;
        //记录是否使用I2C设备读写
        public bool m_open = false;

        private static XChip_Lite_AE _instance;

        public static XChip_Lite_AE Instance(SerialPort _sPortTemp, addressregisterManager _aBll)
        {
            if (_instance == null)
            {
                _instance = new XChip_Lite_AE(_sPortTemp, _aBll);
            }
            return _instance;
        }

        public XChip_Lite_AE()
        {
            InitializeComponent();
        }
        public XChip_Lite_AE(SerialPort _sPortTemp, addressregisterManager _aBll)
        {
               sPortTemp = _sPortTemp;
               aBll = _aBll;
            XChipAe = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "ae" || c.Classify == "Common"));
            InitializeComponent();
        }
        private Control[] Controls_Find(string str)
        {
            //var t =   this.Controls.Find(type, true);
            List<Control> listc = new List<Control>();
            System.Reflection.FieldInfo[] fieldInfo = this.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            for (int i = 0; i < fieldInfo.Length; i++)
            {
                if (fieldInfo[i].Name.ToLower().Contains(str.ToLower()))
                {
                    listc.AddRange(this.Controls.Find(fieldInfo[i].Name, true));
                    break;
                }
            }
            return listc.ToArray();
        }

        private void simpleButton5_Click(object sender, EventArgs e)
        {
            if (IsUsbOpen || m_open)
            {

            }
            else
            {
                if (!sPortTemp.IsOpen)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                    return;
                }
            }
            //ulong nDaymode = 0;
            isRead = true;
            WaitBefore.Start("读取中", "正在加载中", 10);
            GetISPAeSetting();

        }

        public void GetISPAeSetting()
        {
            #region 勾选框获取

            try
            {

                //当存在pwm或ircut时才能切换白天黑夜
                //if ((GetAERegister("PWM_Ctrl_Enable") == 1) || (GetAERegister("bISP0IRCEnable") == 1))
                //{
                //    Mono_mode_en.Enabled = true;
                //    Mono_mode_en.Checked = (GetAERegister("Mono_mode_en") & 0x80) == 0x80;
                //}
                //else
                //{
                //    Mono_mode_en.Enabled = false;
                //    Mono_mode_en.Checked = false;
                //}
				if ((GetAERegister("Target_switch") == 0))
                {
                    COMBO_GAMMAPRE.ItemIndex = 0;
                    TargetSwitchEnShowHide(false);
                }
                else if((GetAERegister("Target_switch") == 1))
                {
                    COMBO_GAMMAPRE.ItemIndex = 1;
                    TargetSwitchEnShowHide(true);
                }
                else if ((GetAERegister("Target_switch") == 2))
                {
                    COMBO_GAMMAPRE.ItemIndex = 2;
                    TargetSwitchEnShowHide(true);
                }
                else
                {
                    COMBO_GAMMAPRE.ItemIndex = -1;
                }
                var WEIGHTENABLE_EN = (GetAERegister("AE_WEIGHTENABLE_EN")) & 0x01;
                long newmode_en = 0;

                newmode_en = ((SerialSend.ReadRegister((Convert.ToUInt32("2000023f", 16)), sPortTemp)) & 0x01);

                //if (WEIGHTENABLE_EN == 1 && newmode_en == 1)
                //{

                //    SerialSend.WriteRegister(0x2000013a, 0x0, sPortTemp);


                //    WEIGHTENABLE_EN = 0;
                //}
                if (newmode_en == 1)
                {
                    Newmode_en.CheckState = CheckState.Checked;

                }
                else
                {
                    Newmode_en.CheckState = CheckState.Unchecked;
                }
                //if (WEIGHTENABLE_EN == 1)
                //{
                //    AE_WEIGHTENABLE_EN.CheckState = CheckState.Checked;
                //}
                //else
                //{
                //    AE_WEIGHTENABLE_EN.CheckState = CheckState.Unchecked;
                //}
                //string scenesMask = Mono_mode_en.Checked ? "night" : "day";
                long nMask = 0;
                nMask = GetAERegister("Scene_Switch_En");

                List<string> NameList = new List<string>();

                List<string> NameListsss = new List<string>();
                for (int i = 0; i < XChipAe.Count; i++)
                {

                    var type = AppHelper.NameTypeToString(XChipAe[i].NameType);
                    if (type.ToLower().Contains(("AE_TARGET_JUDGE_TAB0").ToLower()))
                    {

                    }
                    if (NameList.Exists(c => c.ToLower().Contains(type)))
                    {
                        continue;
                    }
                    if (XChipAe[i].NameType.Contains("AE_WEIGHT_") && !XChipAe[i].NameType.Contains("_EN"))
                    {
                        continue;
                    }
                    ////加上单选框的_en
                    //if (XChipAe[i].ControlType == "1")
                    //{
                    //    type = XChipAe[i].NameType;
                    //}
                    //if (type.Contains("AE_WIN_WIDTH".ToLower()))
                    //{

                    //}
                    try
                    {
                        var col = Controls_Find(type);
                        foreach (var item in col)
                        {
                            //过滤掉指定寄存器
                            //if (listfiltration.Exists(c => c == item.Name.ToString()))
                            //{
                            //    continue;
                            //}
                            if (item is TextEdit)
                            {
                                item.Text = "";
                                var nList = XChipAe.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.ControlType == "0").ToList();

                                if (nList.Count == 1)
                                {
                                    string Val = "";


                                    Val = SerialSend.ReadRegister(Convert.ToUInt32(nList[0].Address, 16), sPortTemp).ToString("X");

                                    item.Text = BitsHelp.GetBitsToUint(nList[0].Bits, nList[0].Hex, Val).ToString("X");
                                }
                                else
                                {   //处理AE_TARGET_JUDGE_TAB
                                    if (nList.Exists(c => c.NameType.ToLower().Contains("ae_target_judge_tab")))
                                    {
                                        //nList = nList.Where(c => c.NameType.ToLower().Contains(Mode_switch.SelectedIndex == 0 ? "_day" : "_night") && !c.NameType.ToLower().Contains("_set")).ToList();
                                        nList = nList.Where(c => c.NameType.ToLower().Contains("_day") && !c.NameType.ToLower().Contains("_set")).ToList();
                                    }
                                    else if (nList.Exists(c => c.NameType.ToLower().Contains("ae_target_judge_thr")))
                                    {
                                        //nList = nList.Where(c => c.NameType.ToLower().Contains(Mode_switch.SelectedIndex == 0 ? "_day" : "_night") && !c.NameType.ToLower().Contains("_set")).ToList();
                                        nList = nList.Where(c => c.NameType.ToLower().Contains("_day") && !c.NameType.ToLower().Contains("_set")).ToList();
                                    }
                                    else if (nList.Exists(c => c.NameType.ToLower().Contains("ae_weight") ||
                                    c.NameType.ToLower().Contains("start_blk") ||
                                    c.NameType.ToLower().Contains("blk_num")
                                   ))
                                    {
                                        if (nMask == 0)
                                        {
                                            nList = nList.Where(c => c.NameType.ToLower() == item.Name.ToLower()).ToList();
                                        }
                                        else
                                        {
                                            nMask = GetAERegister("Scene_Mode");
                                            nMask &= 0x3;
                                            if (nMask == 0)
                                            {
                                                //_Day
                                                nList = nList.Where(c => c.NameType.ToLower().Contains($"_day")).ToList();
                                            }
                                            else if (nMask == 1)
                                            {
                                                //_Night
                                                nList = nList.Where(c => c.NameType.ToLower().Contains($"_night")).ToList();
                                            }
                                            else
                                            {
                                                //_Midnight
                                                nList = nList.Where(c => c.NameType.ToLower().Contains($"_midnight")).ToList();
                                            }
                                        }
                                    }
                                    else if (nList.Exists(c => c.NameType.ToLower().Contains("ae_win_width") ||
                                    c.NameType.ToLower().Contains("ae_win_height")))
                                    {
                                        if (nMask == 0)
                                        {
                                            nList = nList.Where(c => c.NameType.ToLower() == $"{item.Name.ToLower()}_h" || c.NameType.ToLower() == $"{item.Name.ToLower()}_l").ToList();
                                        }
                                        else
                                        {
                                            nMask = GetAERegister("Scene_Mode");
                                            nMask &= 0x3;
                                            if (nMask == 0)
                                            {
                                                //_Day
                                                nList = nList.Where(c => c.NameType.ToLower() == $"{item.Name.ToLower()}_h_day" || c.NameType.ToLower() == $"{item.Name.ToLower()}_l_day").ToList();
                                            }
                                            else if (nMask == 1)
                                            {
                                                //_Night
                                                nList = nList.Where(c => c.NameType.ToLower() == $"{item.Name.ToLower()}_h_night" || c.NameType.ToLower() == $"{item.Name.ToLower()}_l_night").ToList();
                                            }
                                            else
                                            {
                                                //_Midnight
                                                nList = nList.Where(c => c.NameType.ToLower() == $"{item.Name.ToLower()}_h_midnight" || c.NameType.ToLower() == $"{item.Name.ToLower()}_l_midnight").ToList();
                                            }
                                        }
                                    }
                                    string Val = string.Empty;

                                    //这里默认为是有多个寄存器  一个文本框的情况进行处理
                                    if (nList.Exists(c => c.NameType.ToLower().Contains("_h")))
                                    {
                                        Val += SerialSend.ReadRegister(Convert.ToUInt32(nList.Find(c => c.NameType.ToLower().Contains("_h")).Address, 16), sPortTemp).ToString("X2");
                                    }

                                    if (nList.Exists(c => c.NameType.ToLower().Contains("_m2")))
                                    {
                                        Val += SerialSend.ReadRegister(Convert.ToUInt32(nList.Find(c => c.NameType.ToLower().Contains("_m2")).Address, 16), sPortTemp).ToString("X2");
                                    }
                                    if (nList.Exists(c => c.NameType.ToLower().Contains("_m1")))
                                    {
                                        Val += SerialSend.ReadRegister(Convert.ToUInt32(nList.Find(c => c.NameType.ToLower().Contains("_m1")).Address, 16), sPortTemp).ToString("X2");
                                    }
                                    if (nList.Exists(c => c.NameType.ToLower().Contains("_m0")))
                                    {
                                        Val += SerialSend.ReadRegister(Convert.ToUInt32(nList.Find(c => c.NameType.ToLower().Contains("_m0")).Address, 16), sPortTemp).ToString("X2");
                                    }
                                    if (nList.Exists(c => c.NameType.ToLower().Contains("_l")))
                                    {
                                        if (nList.Exists(c => c.NameType.ToLower().Substring(c.NameType.Length - 2).Contains("_l")))
                                        {
                                            Val += SerialSend.ReadRegister(Convert.ToUInt32(nList.Find(c => c.NameType.ToLower().Substring(c.NameType.Length - 2).Contains("_l")).Address, 16), sPortTemp).ToString("X2");
                                        }
                                        else
                                        {
                                            Val += SerialSend.ReadRegister(Convert.ToUInt32(nList.Last(c => c.NameType.ToLower().Contains("_l")).Address, 16), sPortTemp).ToString("X2");
                                        }
                                    }

                                    if (nList.Count == 1)
                                    {
                                        Val = SerialSend.ReadRegister(Convert.ToUInt32(nList[0].Address, 16), sPortTemp).ToString("X");
                                        item.Text = BitsHelp.GetBitsToUint(nList[0].Bits, nList[0].Hex, Val).ToString("X");
                                        if (nList[0].NameType.ToLower().Contains("ae_weight"))
                                        {
                                            var NameArr = nList[0].NameType.Split('_');
                                            setLabTxtJiu(int.Parse(NameArr.Last()) - 1, Val);
                                        }

                                        continue;
                                    }
                                    if (listFiltration.Exists(c => c == item.Name.ToString()))
                                    {
                                        item.Text = Convert.ToInt32(Val, 16).ToString();
                                    }
                                    else
                                    {
                                        item.Text = Val;
                                    }

                                }
                            }
                            else if (item is CheckEdit)
                            {
                                var nList = XChipAe.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.ControlType == "1").ToList();
                                if (nList.Count == 1)
                                {
                                    string Val = "";
                                    if (IsUsbOpen)
                                    {
                                        uint pVule = 0x0;
                                        bool isbool;
                                        if (IntPtr.Size == 8)
                                        {
                                            isbool = XChipUSB.UsbRegisterRead64(Convert.ToUInt32(nList[0].Address, 16), ref pVule, false);
                                        }
                                        else
                                        {
                                            isbool = XChipUSB.UsbRegisterRead86(Convert.ToUInt32(nList[0].Address, 16), ref pVule, false);
                                        }
                                        var addrstr = Convert.ToUInt32(Convert.ToUInt32(nList[0].Address, 16));
                                        SerialSend.Sendser("R:0x" + addrstr.ToString("X2"), pVule.ToString("X2"));
                                        Val = pVule.ToString("X");

                                    }
                                    else
                                    {
                                        Val = SerialSend.ReadRegister(Convert.ToUInt32(nList[0].Address, 16), sPortTemp).ToString("X");
                                    }
                                    (item as CheckEdit).Checked = BitsHelp.GetBitsCheckedToBool(nList[0].Bits, nList[0].Hex, Val) == 0;
                                }
                            }


                        }

                        //ae_win_width\ae_win_height 重复名称太多 这里不过滤 数据进行强匹配
                        if (type.ToLower().Contains("ae_win_width") || type.ToLower().Contains("ae_win_height"))
                        {
                            continue;
                        }
                        NameList.Add(type);
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                }




                //这个测光窗口只读写20000078开始的这组寄存器
                //for (int i = 0; i < 64; i++)
                //{
                //    var nTemp = GetAERegister($"AE_WEIGHT_{(i + 1).ToString("D2")}");
                //    SetAEWin(i, nTemp);
                //}

                var nValue = GetAERegister("Flicker5060SW");
                if (nValue == 0) //disable
                {
                    checkedListBoxControl2.Items[0].CheckState = CheckState.Unchecked;
                    checkedListBoxControl2.Items[1].CheckState = CheckState.Unchecked;
                }
                else if (nValue == 1) //60hz
                {
                    checkedListBoxControl2.Items[0].CheckState = CheckState.Unchecked;
                    checkedListBoxControl2.Items[1].CheckState = CheckState.Checked;
                }
                else if (nValue == 2) //50hz
                {
                    checkedListBoxControl2.Items[0].CheckState = CheckState.Checked;
                    checkedListBoxControl2.Items[1].CheckState = CheckState.Unchecked;
                }
                #endregion

                //aewinTxt.Text = $"{AE_WIN_WIDTH.Text} x {AE_WIN_HEIGHT.Text}";

                //显示ae窗体文本值
                #region 目标亮度 增益 曝光行的读
                long ae_b = 0;

                ae_b = ae_b = SerialSend.ReadRegister(0x20000b91, sPortTemp);

                //if (ae_b == 0x01 || ae_b == 0x2)
                //{
                //    AE_BRIGHTNESS.Enabled = true;
                //}
                //else
                //{
                //    AE_BRIGHTNESS.Enabled = false;
                //}
                //HDRAE_SpeedModeradio
                //AE_SpeedModeradio.SelectedIndex = (int)SerialSend.ReadRegister(Convert.ToUInt32(XChipAe.Find(c => c.NameType.ToLower().Contains("speed_mode_man".ToLower()) && c.ControlType == "0").Address, 16), sPortTemp);

                #endregion



                m_nFollowArea[0] = SerialSend.ReadRegister(0x200000b8, sPortTemp);
                m_nFollowArea[1] = SerialSend.ReadRegister(0x200000b9, sPortTemp);
                m_nFollowArea[2] = SerialSend.ReadRegister(0x200000ba, sPortTemp);
                m_nFollowArea[3] = SerialSend.ReadRegister(0x200000bb, sPortTemp);
                m_nFollowArea[4] = SerialSend.ReadRegister(0x200000bc, sPortTemp);
                m_nFollowArea[5] = SerialSend.ReadRegister(0x200000bd, sPortTemp);
                m_nFollowArea[6] = SerialSend.ReadRegister(0x200000be, sPortTemp);
                m_nFollowArea[7] = SerialSend.ReadRegister(0x200000bf, sPortTemp);

                //CheckFollowArea(m_nFollowArea);
                //AE_FOLLOW_AREA.Text = (m_nFollowArea[7] << 56 | m_nFollowArea[6] << 48 | m_nFollowArea[5] << 40 | m_nFollowArea[4] << 32 | m_nFollowArea[3] << 24 | m_nFollowArea[2] << 16 | m_nFollowArea[1] << 8 | m_nFollowArea[0]).ToString("X15");

                simpleButton1_Click(null, null);//Cur_scene Get
                isRead = true;
                simpleButton3_Click(null, null);//Final_cal Get
                isRead = false;



                if (speed_mode_man_en.Checked)
                {
                    SetAeRegister("speed_mode_man", 0x01);
                }
                EnShowHide();
            }
            catch (Exception)
            {
            }
            finally
            {
                isRead = false;
                WaitBefore.Close();
            }
        }

        //private void SetAEWin(int i, long nTemp)
        //{
        //    foreach (Control item in panelControl5.Controls)
        //    {
        //        if (item is TextEdit)
        //        {
        //            if (item.Name == $"AE_WEIGHT_{(i + 1).ToString("D2")}")
        //            {
        //                item.Text = (nTemp & 0x7F).ToString("X2");
        //            }
        //        }
        //    }
        //}
        //void CheckFollowArea(long[] m_nFollowArea) {
        //    AE_WT_0_EN.Checked = (m_nFollowArea[0] & 1) == 1 ? true : false;
        //    AE_WT_1_EN.Checked = (m_nFollowArea[0] & 2) >> 1 == 1 ? true : false;
        //    AE_WT_2_EN.Checked = ((m_nFollowArea[0] & 4) >> 2) == 1 ? true : false;
        //    AE_WT_3_EN.Checked = (m_nFollowArea[0] & 8) >> 3 == 1 ? true : false;
        //    AE_WT_4_EN.Checked = (m_nFollowArea[0] & 16) >> 4 == 1 ? true : false;
        //    AE_WT_5_EN.Checked = (m_nFollowArea[0] & 32) >> 5 == 1 ? true : false;
        //    AE_WT_6_EN.Checked = (m_nFollowArea[0] & 64) >> 6 == 1 ? true : false;
        //    AE_WT_7_EN.Checked = (m_nFollowArea[0] & 128) >> 7 == 1 ? true : false;

        //    AE_WT_8_EN.Checked = (m_nFollowArea[1] & 1) == 1 ? true : false;
        //    AE_WT_9_EN.Checked = (m_nFollowArea[1] & 2) >> 1== 1 ? true : false;
        //    AE_WT_10_EN.Checked = (m_nFollowArea[1] & 4) >> 2 == 1 ? true : false;
        //    AE_WT_11_EN.Checked = (m_nFollowArea[1] & 8) >> 3 == 1 ? true : false;
        //    AE_WT_12_EN.Checked = (m_nFollowArea[1] & 16) >> 4 == 1 ? true : false;
        //    AE_WT_13_EN.Checked = (m_nFollowArea[1] & 32) >> 5 == 1 ? true : false;
        //    AE_WT_14_EN.Checked = (m_nFollowArea[1] & 64) >> 6 == 1 ? true : false;
        //    AE_WT_15_EN.Checked = (m_nFollowArea[1] & 128) >>7 == 1 ? true : false;

        //    AE_WT_16_EN.Checked = (m_nFollowArea[2] & 1) == 1 ? true : false;
        //    AE_WT_17_EN.Checked = (m_nFollowArea[2] & 2) >> 1 == 1 ? true : false;
        //    AE_WT_18_EN.Checked = (m_nFollowArea[2] & 4) >> 2== 1 ? true : false;
        //    AE_WT_19_EN.Checked = (m_nFollowArea[2] & 8) >> 3 == 1 ? true : false;
        //    AE_WT_20_EN.Checked = (m_nFollowArea[2] & 16) >> 4 == 1 ? true : false;
        //    AE_WT_21_EN.Checked = (m_nFollowArea[2] & 32) >> 5 == 1 ? true : false;
        //    AE_WT_22_EN.Checked = (m_nFollowArea[2] & 64) >> 6 == 1 ? true : false;
        //    AE_WT_23_EN.Checked = (m_nFollowArea[2] & 128) >> 7 == 1 ? true : false;

        //    AE_WT_24_EN.Checked = (m_nFollowArea[3] & 1) == 1 ? true : false;
        //    AE_WT_25_EN.Checked = (m_nFollowArea[3] & 2) >> 1 == 1 ? true : false;
        //    AE_WT_26_EN.Checked = (m_nFollowArea[3] & 4) >> 2 == 1 ? true : false;
        //    AE_WT_27_EN.Checked = (m_nFollowArea[3] & 8) >> 3 == 1 ? true : false;
        //    AE_WT_28_EN.Checked = (m_nFollowArea[3] & 16) >> 4 == 1 ? true : false;
        //    AE_WT_29_EN.Checked = (m_nFollowArea[3] & 32) >> 5 == 1 ? true : false;
        //    AE_WT_30_EN.Checked = (m_nFollowArea[3] & 64) >> 6 == 1 ? true : false;
        //    AE_WT_31_EN.Checked = (m_nFollowArea[3] & 128) >> 7 == 1 ? true : false;

        //    AE_WT_32_EN.Checked = (m_nFollowArea[4] & 1) == 1 ? true : false;
        //    AE_WT_33_EN.Checked = (m_nFollowArea[4] & 2) >> 1 == 1 ? true : false;
        //    AE_WT_34_EN.Checked = (m_nFollowArea[4] & 4) >> 2 == 1 ? true : false;
        //    AE_WT_35_EN.Checked = (m_nFollowArea[4] & 8) >> 3 == 1 ? true : false;
        //    AE_WT_36_EN.Checked = (m_nFollowArea[4] & 16) >> 4 == 1 ? true : false;
        //    AE_WT_37_EN.Checked = (m_nFollowArea[4] & 32) >> 5 == 1 ? true : false;
        //    AE_WT_38_EN.Checked = (m_nFollowArea[4] & 64) >> 6 == 1 ? true : false;
        //    AE_WT_39_EN.Checked = (m_nFollowArea[4] & 128) >> 7 == 1 ? true : false;

        //    AE_WT_40_EN.Checked = (m_nFollowArea[5] & 1) == 1 ? true : false;
        //    AE_WT_41_EN.Checked = (m_nFollowArea[5] & 2) >> 1 == 1 ? true : false;
        //    AE_WT_42_EN.Checked = (m_nFollowArea[5] & 4) >> 2 == 1 ? true : false;
        //    AE_WT_43_EN.Checked = (m_nFollowArea[5] & 8) >> 3 == 1 ? true : false;
        //    AE_WT_44_EN.Checked = (m_nFollowArea[5] & 16) >> 4 == 1 ? true : false;
        //    AE_WT_45_EN.Checked = (m_nFollowArea[5] & 32) >> 5 == 1 ? true : false;
        //    AE_WT_46_EN.Checked = (m_nFollowArea[5] & 64) >> 6 == 1 ? true : false;
        //    AE_WT_47_EN.Checked = (m_nFollowArea[5] & 128) >> 7 == 1 ? true : false;

        //    AE_WT_48_EN.Checked = (m_nFollowArea[6] & 1) == 1 ? true : false;
        //    AE_WT_49_EN.Checked = (m_nFollowArea[6] & 2) >> 1 == 1 ? true : false;
        //    AE_WT_50_EN.Checked = (m_nFollowArea[6] & 4) >> 2 == 1 ? true : false;
        //    AE_WT_51_EN.Checked = (m_nFollowArea[6] & 8) >> 3 == 1 ? true : false;
        //    AE_WT_52_EN.Checked = (m_nFollowArea[6] & 16) >> 4 == 1 ? true : false;
        //    AE_WT_53_EN.Checked = (m_nFollowArea[6] & 32) >> 5 == 1 ? true : false;
        //    AE_WT_54_EN.Checked = (m_nFollowArea[6] & 64) >> 6 == 1 ? true : false;
        //    AE_WT_55_EN.Checked = (m_nFollowArea[6] & 128) >> 7 == 1 ? true : false;

        //    AE_WT_56_EN.Checked = (m_nFollowArea[7] & 1) == 1 ? true : false;
        //    AE_WT_57_EN.Checked = (m_nFollowArea[7] & 2) >> 1 == 1 ? true : false;
        //    AE_WT_58_EN.Checked = (m_nFollowArea[7] & 4) >> 2 == 1 ? true : false;
        //    AE_WT_59_EN.Checked = (m_nFollowArea[7] & 8) >> 3 == 1 ? true : false;
        //    AE_WT_60_EN.Checked = (m_nFollowArea[7] & 16) >> 4 == 1 ? true : false;
        //    AE_WT_61_EN.Checked = (m_nFollowArea[7] & 32) >> 5 == 1 ? true : false;
        //    AE_WT_62_EN.Checked = (m_nFollowArea[7] & 64) >> 6 == 1 ? true : false;
        //    AE_WT_63_EN.Checked = (m_nFollowArea[7] & 128) >> 7 == 1 ? true : false;
        //}
        void TargetSwitchEnShowHide(bool isbool) {
            AE_BRIGHTNESS.Enabled = !isbool;
            Factor.Enabled = isbool;
            AE_TARGET_JUDGE_TAB0.Enabled = isbool;
            AE_TARGET_JUDGE_TAB1.Enabled = isbool;
            AE_TARGET_JUDGE_TAB2.Enabled = isbool;
            AE_TARGET_JUDGE_TAB3.Enabled = isbool;
            AE_TARGET_JUDGE_TAB4.Enabled = isbool;
            AE_TARGET_JUDGE_TAB5.Enabled = isbool;
            AE_TARGET_JUDGE_THR0.Enabled = isbool;
            AE_TARGET_JUDGE_THR1.Enabled = isbool;
            AE_TARGET_JUDGE_THR2.Enabled = isbool;
            AE_TARGET_JUDGE_THR3.Enabled = isbool;
            AE_TARGET_JUDGE_THR4.Enabled = isbool;

        }

        void GetTargetJudgeTable(long dayValue, int index) {
            switch (index)
            {
                case 0:
                    AE_TARGET_JUDGE_TAB0.Text = dayValue.ToString("X2");
                   
                    break;
                case 1:
                    AE_TARGET_JUDGE_TAB1.Text = dayValue.ToString("X2");
                   
                    break;
                case 2:
                    AE_TARGET_JUDGE_TAB2.Text = dayValue.ToString("X2");
                   
                    break;
                case 3:
                    AE_TARGET_JUDGE_TAB3.Text = dayValue.ToString("X2");
                   
                    break;
                case 4:
                    AE_TARGET_JUDGE_TAB4.Text = dayValue.ToString("X2");
                 
                    break;
                case 5:
                    AE_TARGET_JUDGE_TAB5.Text = dayValue.ToString("X2");
                
                    break;
                default:
                    break;
            }
        }

        void GetTargetJudgeThr(long Value,  int index) {
            switch (index)
            {
                case 0:
                    AE_TARGET_JUDGE_THR0.Text = Value.ToString("X2");
                
                    break;
                case 1:
                    AE_TARGET_JUDGE_THR1.Text = Value.ToString("X2");
                   
                    break;
                case 2:
                    AE_TARGET_JUDGE_THR2.Text = Value.ToString("X2");
                   
                    break;
                case 3:
                    AE_TARGET_JUDGE_THR3.Text = Value.ToString("X2");
                    
                    break;
                case 4:
                    AE_TARGET_JUDGE_THR4.Text = Value.ToString("X2");
                 
                    break;
                default:
                    break;
            }
        }


        //final _cal获取
        private void simpleButton3_Click(object sender, EventArgs e)
        {
            if (IsUsbOpen || m_open)
            {

            }
            else
            {
                if (!sPortTemp.IsOpen)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                    return;
                }
            }
            isRead = true;
            try
            {
                var nBHigh = GetAERegister("Factor_h");
                var nHigh = GetAERegister("Factor_m1");
                var nMid = GetAERegister("Factor_m0");
                var nLow = GetAERegister("Factor_l");
                var factor = (nBHigh << 24) | (nHigh << 16) | (nMid << 8) | (nLow); //factor


                Factor.Text = factor.ToString("X2");

                nHigh = GetAERegister("AE_BRIGHTNESS_h");
                nLow = GetAERegister("AE_BRIGHTNESS_l");

                AE_BRIGHTNESS.Text = ((nHigh << 8 & 0xff00) | (nLow & 0xff)).ToString("X4");

                nHigh = GetAERegister("AE_AVRBIRGHT_h");
                nLow = GetAERegister("AE_AVRBIRGHT_l");

                var ae_avrbight = (nHigh << 8 & 0xff00) | (nLow & 0xff);//ae_avrbight            
                nBHigh = GetAERegister("AE_EDIT_GAIN_h");
                nHigh = GetAERegister("AE_EDIT_GAIN_m0");
                nMid = GetAERegister("AE_EDIT_GAIN_m1");
                nLow = GetAERegister("AE_EDIT_GAIN_l");

                var ae_edit_gain = ((nBHigh << 24) | (nHigh << 16) | (nMid << 8) | nLow);//ae_edit_gain

                nBHigh = GetAERegister("AE_EDIT_EXPLINE_h");
                nHigh = GetAERegister("AE_EDIT_EXPLINE_m0");
                nMid = GetAERegister("AE_EDIT_EXPLINE_m1");
                nLow = GetAERegister("AE_EDIT_EXPLINE_l");
                //nHigh = GetAERegister("AE_EDIT_EXPLINE1");
                //nMid = GetAERegister("AE_EDIT_EXPLINE2");
                //nLow = GetAERegister("AE_EDIT_EXPLINE3");

                var ae_edit_expline = (nBHigh << 24) | (nHigh << 16) | (nMid << 8) | (nLow);
                AE_TARGET_CAL_FINAL_CAL.Text = ((ae_avrbight * factor) / ((ae_edit_gain >> 5) * (ae_edit_expline >> 4))).ToString("X2");
                isRead = false;
            }
            catch (Exception)
            {

            }
            finally
            {
                isRead = false;
            }

            
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            //long m_nDayMode;
            isRead = true;
            long nMask = (GetAERegister("Scene_Mode") & 0x3);
            if (nMask == 0)
            {
                AE_radiomode.SelectedIndex = 0;
            }
            else if (nMask == 1)
            {
                AE_radiomode.SelectedIndex = 1;
            }
            else if (nMask == 2)
            {
                AE_radiomode.SelectedIndex = 2;
            }
            //Scene_Switch_En
            if ((GetAERegister("Scene_Switch_En") & 0x12) == 0x12)
            {
                checkedListBoxControl1.Items[0].CheckState = CheckState.Checked;
                checkedListBoxControl1.Items[1].Enabled = true;               
            }
            else
            {
                checkedListBoxControl1.Items[0].CheckState = CheckState.Unchecked;
                checkedListBoxControl1.Items[1].Enabled = false;
                AE_radiomode.Enabled = false;
            }
            //Debug_En
            if ((GetAERegister("Debug_En") & 0x02) == 0x00)
            {
                checkedListBoxControl1.Items[1].CheckState = CheckState.Checked;
                if (checkedListBoxControl1.Items[0].CheckState == CheckState.Checked)
                {
                    AE_radiomode.Enabled = true;
                }
            }
            else
            {
                checkedListBoxControl1.Items[1].CheckState = CheckState.Unchecked;
                AE_radiomode.Enabled = false;
            }
            isRead = false;             
        }


        private void XChip_AE_Load(object sender, EventArgs e)
        {
            List<TreeListValue> GammaList = new List<TreeListValue>();
            GammaList.Add(new TreeListValue() { DisplayMember = "off", ValueMember = "0" });
            GammaList.Add(new TreeListValue() { DisplayMember = "cal_BV", ValueMember = "1" });
            GammaList.Add(new TreeListValue() { DisplayMember = "exp", ValueMember = "2" });
            COMBO_GAMMAPRE.Properties.ValueMember = "ValueMember";
            COMBO_GAMMAPRE.Properties.DisplayMember = "DisplayMember";
            COMBO_GAMMAPRE.Properties.DataSource = GammaList;
            COMBO_GAMMAPRE.ItemIndex = -1;
            Task.Run(() =>
            {

                this.Invoke((EventHandler)delegate
                {

                    //五列
                    int totalloc = 8;
                    //宽高
                    int txtW = 40;
                    int txtH = 40;
                    //生成个数64
                    for (int i = 0; i < 64; i++)
                    {
                        int row = i / totalloc;//行号
                                               //1/3=0,2/3=0,3/3=1;
                        int loc = i % totalloc;//列号

                        int txtViewY = row * txtH + 16;
                        int txtViewX = txtW * loc + 36;
                        DevExpress.XtraEditors.LabelControl labelTxt = new LabelControl();
                        labelTxt.Appearance.Font = new System.Drawing.Font("Tahoma", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                        labelTxt.Appearance.ForeColor = System.Drawing.Color.White;
                        labelTxt.Appearance.Options.UseFont = true;
                        labelTxt.Appearance.Options.UseForeColor = true;
                        labelTxt.AutoSizeMode = DevExpress.XtraEditors.LabelAutoSizeMode.None;
                        labelTxt.Location = new System.Drawing.Point(txtViewX, txtViewY);
                        labelTxt.Name = $"labTxtJiu_{i}";
                        labelTxt.Padding = new System.Windows.Forms.Padding(10);
                        labelTxt.Size = new System.Drawing.Size(40, 40);
                        labelTxt.TabIndex = 0;
                        labelTxt.Tag = i;
                        labelTxt.Text = i.ToString("00");
                        labelTxt.Paint += new System.Windows.Forms.PaintEventHandler(this.labelControl25_Paint);
                        //this.panelControl5.Controls.Add(labelTxt);
                       


                    }
                    //for (int i = 0; i < 64; i++)
                    //{
                    //    foreach (Control item in panelControl5.Controls)
                    //    {
                    //        if ($"AE_WEIGHT_{(i + 1).ToString("D2")}" == item.Name)
                    //        {
                    //            item.Invoke((EventHandler)delegate { item.Enabled = false; });
                    //        }
                    //    }
                    //}

                    for (int i = 0; i < XChipAe.Count; i++)
                    {

                        var type = AppHelper.NameTypeToString(XChipAe[i].NameType);
                        //try
                        //{
                        //加上单选框的_en
                        if (XChipAe[i].ControlType == "1")
                        {
                            type = XChipAe[i].NameType;
                        }
                        var col = this.Controls.Find(type, true);


                        foreach (var item in col)
                        {
                            if (item is TextEdit)
                            {
                                //声明一个SuerToolTip
                                DevExpress.Utils.SuperToolTip graduationBatchSuperToolTip = new DevExpress.Utils.SuperToolTip();
                                //声明一个ToolTipTitleItem,保存标题
                                DevExpress.Utils.ToolTipTitleItem toolTipTitleItem = new DevExpress.Utils.ToolTipTitleItem();
                                String tooltips = "";

                                if (!string.IsNullOrEmpty(XChipAe[i].Bit_Wide))
                                {
                                    tooltips += $"位宽={XChipAe[i].Bit_Wide}";
                                }
                                if (!string.IsNullOrEmpty(XChipAe[i].Bit_Accuracy))
                                {
                                    tooltips += $"|精度={XChipAe[i].Bit_Accuracy}";
                                }
                                //设置标题文字
                                toolTipTitleItem.Text = tooltips;
                                //把标题添加到SuerToolTip
                                graduationBatchSuperToolTip.Items.Add(toolTipTitleItem);
                                //指定控件的SuperTip

                                ((TextEdit)item).SuperTip = graduationBatchSuperToolTip;
                                //正则判断
                                ((TextEdit)item).Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx;
                                ((TextEdit)item).Properties.Mask.EditMask = BitsHelp.GetBitsToMask(XChipAe[i].Bits, XChipAe[i].Bit_Wide, XChipAe.Where(c => c.NameType.ToLower().Contains(type) && c.ControlType == "0").Select(c => c.Bits).ToList());

                            }
                        }


                        //}
                        //catch (Exception)
                        //{
                        //    continue;
                        //}
                    }
                });


            });
           
        }

        private void labelControl25_Paint(object sender, PaintEventArgs e)
        {
            var txtTemp = sender as DevExpress.XtraEditors.LabelControl;
            Pen pen = new Pen(Color.DarkRed, 2);
            pen.DashStyle = DashStyle.Dash;
            e.Graphics.DrawRectangle(pen, txtTemp.DisplayRectangle);
        }


        /// <summary>
        /// 根据tag 设置文本框的的文本
        /// </summary>
        /// <param name="temp"></param>
        /// <param name="txt"></param>
        void setLabTxtJiu(object temp, string txt)
        {
            //foreach (Control item in panelControl5.Controls)
            //{
            //    if (item.Name == $"labTxtJiu_{temp}")
            //    {
            //        item.Text = txt.PadLeft(2, '0');
            //    }
            //}
        }
        //void SetAE_WEIGHT(object temp, string txt)
        //{
        //        foreach (Control item in panelControl5.Controls)
        //    {
        //        if ($"AE_WEIGHT_{(int.Parse(temp.ToString()) + 1).ToString("D2")}" == item.Name)
        //        {
        //            var nValue = Convert.ToInt32(txt, 16) & 0x7f;
        //            item.Text = nValue.ToString("X");
        //        }
        //    }
        //}
       

        string GetCheckListBox(DevExpress.XtraEditors.CheckedListBoxControl sender) {
            string checkTxt = string.Empty;
            
            int count = sender.CheckedIndices.Count;//获得勾选的所有条集合
            var chkIndexCollection = sender.CheckedIndices;//获得所有选中行的集合

            for (int i = 0; i < count; i++)
            {
                checkTxt += chkIndexCollection[i].ToString();
            }
            return checkTxt;
        }


        #region 设置单选框


        private void checkedListBoxControl2_ItemChecking(object sender, DevExpress.XtraEditors.Controls.ItemCheckingEventArgs e)
        {
          
            var checkBox = sender as DevExpress.XtraEditors.CheckedListBoxControl;
            if (checkBox.CheckedItems.Count > 0)
            {
                for (int i = 0; i < checkBox.Items.Count; i++)
                {
                    if (i != e.Index)
                    {
                        checkBox.SetItemChecked(i, false);
                    }
                }
            }
        }
        #endregion       
        #region 勾选框changed
        //Scence Switch & Debug_En
        private void checkedListBoxControl1_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {
            if (isRead) return;
            //Scence Switch En 
            var nTemp1 = GetAERegister("Scene_Switch_En");
            // Debug en
            var nTemp2 = GetAERegister("Debug_En");
            if (checkedListBoxControl1.Items[0].CheckState == CheckState.Checked)//S勾选
            {
                SetAeRegister("Scene_Switch_En", 0x12);
                checkedListBoxControl1.Items[1].Enabled = true;                
                if (checkedListBoxControl1.Items[1].CheckState == CheckState.Checked)//D勾选
                {
                    AE_radiomode.Enabled = true;
                }
            }
            else  //S不勾选
            {
                SetAeRegister("Scene_Switch_En", 0x00);
                checkedListBoxControl1.Items[1].Enabled = false;
                AE_radiomode.Enabled = false;               
            }


            if (checkedListBoxControl1.Items[1].CheckState == CheckState.Checked)//D勾选
            {
                SetAeRegister("Debug_En", (ulong)(nTemp2 & 0xfd));
                if (checkedListBoxControl1.Items[0].CheckState == CheckState.Checked)//S勾选
                {
                    AE_radiomode.Enabled = true;
                }
            }
            else//D不勾选
            {
                SetAeRegister("Debug_En", (ulong)(nTemp2 | 0x02));
                AE_radiomode.Enabled = false;
            }
        }
        //Scence Mode
        private void AE_radiomode_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isRead) return;
            var aeradio = sender as DevExpress.XtraEditors.RadioGroup;
            if (aeradio.EditValue.ToString() == "1")
            {
                SetAeRegister("Scene_Mode", 0x00);
            }
            else if (aeradio.EditValue.ToString() == "2")
            {
                SetAeRegister("Scene_Mode", 0x01);
            }
            else if (aeradio.EditValue.ToString() == "3")
            {
                SetAeRegister("Scene_Mode", 0x02);
            }
            simpleButton5_Click(null,null);

        }
        //TARGET_SWITCH_MODE_EN
        private void TARGET_SWITCH_MODE_EN_CheckedChanged(object sender, EventArgs e)
        {
            if (isRead) return;
            var nTemp = GetAERegister("Target_switch_en");
            if (Target_switch_en.CheckState == CheckState.Checked)
            {
                SetAeRegister("Target_switch_en", (ulong)(nTemp | 0x01));
                TargetSwitchEnShowHide(true);
            }
            else
            {
                SetAeRegister("Target_switch_en", (ulong)(nTemp & 0xfe));
                TargetSwitchEnShowHide(false);
            }
        }
        //AE_ISP_GAINENABLE_EN
        private void AE_ISP_GAINENABLE_EN_CheckedChanged(object sender, EventArgs e)
        {
            //if (isRead) return;
            //var nTemp = GetAERegister("Isp_Gain_en");
            //if (Isp_Gain_en.CheckState == CheckState.Checked)
            //{
            //    SetAeRegister("Isp_Gain_en", (ulong)(nTemp | 0x20));
            //}
            //else
            //{
            //    SetAeRegister("Isp_Gain_en", (ulong)(nTemp & 0xdf));
            //}
        }
        //Flicker
        private void checkedListBoxControl2_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {
            if (isRead) return;
            if (checkedListBoxControl2.Items[0].CheckState == CheckState.Checked)//50hz
            {
                SetAeRegister("Flicker5060SW", (ulong)0x02);
            }
            if (checkedListBoxControl2.Items[1].CheckState == CheckState.Checked)//60hz
            {
                SetAeRegister("Flicker5060SW", (ulong)0x01);
            }
            if ((checkedListBoxControl2.Items[1].CheckState == CheckState.Unchecked) && (checkedListBoxControl2.Items[0].CheckState == CheckState.Unchecked))//均不勾选
            {
                SetAeRegister("Flicker5060SW", (ulong)0x00);
            }
            EnShowHide();
        }
        #endregion

        /// <summary>
        /// 权重
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AE_WEIGHT_EN_CheckedChanged(object sender, EventArgs e)
        {
            //if (AE_WEIGHT_EN.Checked)
            //{
               
            //    for (int i = 0; i < 64; i++)
            //    {
            //        foreach (Control item in panelControl5.Controls)
            //        {
            //            if ($"AE_WEIGHT_{(i + 1).ToString("D2")}" == item.Name)
            //            {
            //                item.Enabled = true;
            //            }
            //        }
            //    }
            //}
            //else {
            //    for (int i = 0; i < 64; i++)
            //    {
            //        foreach (Control item in panelControl5.Controls)
            //        {
            //            if ($"AE_WEIGHT_{(i + 1).ToString("D2")}" == item.Name)
            //            {
            //                item.Enabled = false;
            //            }
            //        }
            //    }
            //}
        }
        /// <summary>
        /// 关注使能区
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AE_WEIGHTENABLE_EN_CheckedChanged(object sender, EventArgs e)
        {           
            //if (AE_WEIGHTENABLE_EN.Checked)
            //{
            //    //Newmode_en.CheckState = CheckState.Unchecked;
            //    for (int i = 0; i < 64; i++)
            //    {
            //        foreach (Control item in panelControl5.Controls)
            //        {
            //            if ($"AE_WT_{i}_EN" == item.Name)
            //            {
            //                item.Enabled = true;
            //            }
            //        }
            //    }
            //}
            //else {
            //    //Newmode_en.CheckState = CheckState.Checked;
            //    //SerialSend.WriteRegister(, 0x0, sPortTemp);
            //    for (int i = 0; i < 64; i++)
            //    {
            //        foreach (Control item in panelControl5.Controls)
            //        {
            //            if ($"AE_WT_{i}_EN" == item.Name)
            //            {
            //                item.Enabled = false;
            //            }
            //        }
            //    }
            //}
            //if (isRead) return;
            //if (XChipAe.Exists(c => c.NameType.ToLower() == ("Magnitude_en").ToLower()))
            //{
            //    var model = XChipAe.Find(c => c.NameType.ToLower() == ("Magnitude_en").ToLower());
            //    var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
            //    var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), AE_WEIGHTENABLE_EN.Checked);
            //    SetAeRegister(model, Val);
            //}

        }
        /// <summary>
        /// area 下发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AE_FOLLOW_AREA_EditValueChanged(object sender, EventArgs e)
        {
            //if (isRead) return;
            //var m_nFllowArea = Convert.ToInt32(AE_FOLLOW_AREA.Text, 16);
            //SetAeRegister("AE_FOLLOW_AREA_l", (ulong)(m_nFllowArea & 0xff));
            //SetAeRegister("AE_FOLLOW_AREA_m1", (ulong)((m_nFllowArea >> 8) & 0xff));
            //SetAeRegister("AE_FOLLOW_AREA_m0", (ulong)((m_nFllowArea >> 16) & 0xff));
            //SetAeRegister("AE_FOLLOW_AREA_h", (ulong)((m_nFllowArea >> 24) & 0xff));
        }
        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton4_Click(object sender, EventArgs e)
        {
            SetAeRegister("Refresh", 0x01);
        }

        private void AE_WT_EN1_CheckedChanged(object sender, EventArgs e)
        {
            if (isRead) return;
            var TempCheck = sender as DevExpress.XtraEditors.CheckEdit;
            //for (int i = 0; i < 64; i++)
            //{
            //    foreach (Control item in panelControl5.Controls)
            //    {
            //        // var aaaa = Convert.ToUInt32(AE_FOLLOW_AREA.Text, 16);
            //        //if ($"AE_WT_EN{i + 1}" == item.Name && (TempCheck.Name == item.Name))
            //        if (TempCheck.Name == item.Name)
            //        {
            //            if (TempCheck.CheckState == CheckState.Checked)
            //            {
            //                AE_FOLLOW_AREA.Text = (Convert.ToUInt64(AE_FOLLOW_AREA.Text, 16) | (uint)(0x01 << i)).ToString("X15");
            //            }
            //            else
            //            {
            //                AE_FOLLOW_AREA.Text = (Convert.ToUInt64(AE_FOLLOW_AREA.Text, 16) & (uint)~(0x01 << i)).ToString("X15");
            //            }
            //        }
            //    }
            //}

            int index = int.Parse(TempCheck.Name.Replace("AE_WT_", "").Replace("_EN", ""));
            if (XChipAe.Exists(c => c.NameType.ToLower() == ("nAttentionCfg"+ index.ToString()).ToLower()))
            {
                var model = XChipAe.Find(c => c.NameType.ToLower() == ("nAttentionCfg" + index.ToString()).ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), TempCheck.Checked);
                SetAeRegister(model, Val);
            }

            //m_nFollowArea[0] = SerialSend.ReadRegister(0x200000b8, sPortTemp);
            //m_nFollowArea[1] = SerialSend.ReadRegister(0x200000b9, sPortTemp);
            //m_nFollowArea[2] = SerialSend.ReadRegister(0x200000ba, sPortTemp);
            //m_nFollowArea[3] = SerialSend.ReadRegister(0x200000bb, sPortTemp);
            //m_nFollowArea[4] = SerialSend.ReadRegister(0x200000bc, sPortTemp);
            //m_nFollowArea[5] = SerialSend.ReadRegister(0x200000bd, sPortTemp);
            //m_nFollowArea[6] = SerialSend.ReadRegister(0x200000be, sPortTemp);
            //m_nFollowArea[7] = SerialSend.ReadRegister(0x200000bf, sPortTemp);
            //AE_FOLLOW_AREA.Text = (m_nFollowArea[7] << 56 | m_nFollowArea[6] << 48 | m_nFollowArea[5] << 40 | m_nFollowArea[4] << 32 | m_nFollowArea[3] << 24 | m_nFollowArea[2] << 16 | m_nFollowArea[1] << 8 | m_nFollowArea[0]).ToString("X15");


        }
        private long GetAERegister(string nameType)
        {
            if (IsUsbOpen || m_open)
            {

            }
            else
            {
                if (!sPortTemp.IsOpen)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                    return -1;
                }
            }
            if (IsUsbOpen)
            {
                uint pVule = 0x0;
                bool isbool;
                if (IntPtr.Size == 8)
                {
                    isbool = XChipUSB.UsbRegisterRead64(Convert.ToUInt32(XChipAe.Where(c => c.NameType.ToLower() == nameType.ToLower()).First().Address, 16), ref pVule, false);
                }
                else
                {
                    isbool = XChipUSB.UsbRegisterRead86(Convert.ToUInt32(XChipAe.Where(c => c.NameType.ToLower() == nameType.ToLower()).First().Address, 16), ref pVule, false);
                }
                var addrstr = Convert.ToUInt32(XChipAe.Where(c => c.NameType.ToLower() == nameType.ToLower()).First().Address, 16);
                SerialSend.Sendser("R:0x" + addrstr.ToString("X2"), pVule.ToString("X2"));
                return (long)pVule;
            }
            else
            {
                if (XChipAe.Where(c => c.NameType.ToLower() == nameType.ToLower()).Count() > 0)
                    return SerialSend.ReadRegister(Convert.ToUInt32(XChipAe.Where(c => c.NameType.ToLower() == nameType.ToLower()).First().Address, 16), sPortTemp);
                else { DevExpress.XtraEditors.XtraMessageBox.Show($"未找到{nameType}"); return -1; }
            }
            
        }
        private void SetAeRegister(string nameType, ulong add)
        {
            if (isRead) return;
            if (IsUsbOpen)
            {
                if (null != BinHelp.usbList)
                {
                    uint pVule = 0x0;
                    var isbool = false;
                    if (IntPtr.Size == 8)
                    {
                        isbool = XChipUSB.UsbRegisterRead64(BinHelp.CurrentSize, ref pVule, true);
                        SerialSend.Sendser("R:0x" + (BinHelp.CurrentSize).ToString("X2"), pVule.ToString("X2"));


                    }
                    else
                    {
                        isbool = XChipUSB.UsbRegisterRead86(BinHelp.CurrentSize, ref pVule, true);
                        SerialSend.Sendser("R:0x" + (BinHelp.CurrentSize).ToString("X2"), pVule.ToString("X2"));
                    }
                    var chang = (int)(pVule >> 16); // 使用右移16位操作来获取高16位  
                    var kuang = (int)(pVule & 0xFFFF); // 使用位与操作来获取低16位  

                    BinHelp.SstBinUsbUpdateData(ref BinHelp.BinList, XChipAe.Where(c => c.NameType.ToLower() == nameType.ToLower()).First().Address, add.ToString("X"), chang, kuang);
                }
            }
            else if (m_open)
            {

            }
            else
            {
                if (!sPortTemp.IsOpen)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                    return;
                }
                if (null != BinHelp.lspList && XChipAe.Where(c => c.NameType.ToLower() == nameType.ToLower()).Count() > 0)
                { //编写Bin更新数据
                    var temp = SerialSend.ReadRegister(BinHelp.BootSettingIdx, sPortTemp);
                    BinHelp.SetBinUpdateData(ref BinHelp.BinList, XChipAe.Where(c => c.NameType.ToLower() == nameType.ToLower()).First().Address, add.ToString("X"), (uint)temp);
                }
            }
          
           
            if (XChipAe.Where(c => c.NameType.ToLower() == nameType.ToLower()).Count() > 0)
            {
                SerialSend.WriteRegister(Convert.ToUInt32(XChipAe.Where(c => c.NameType.ToLower() == nameType.ToLower()).First().Address, 16), add, sPortTemp);
               
            }
            
            
        }
        private void SetAeRegister(addressregister model, ulong add, bool isCheck = true)
        {
            if (isRead) return;
            if (IsUsbOpen)
            {
               
                if (null != BinHelp.usbList)
                {
                    uint pVule = 0x0;
                    var isbool = false;
                    if (IntPtr.Size == 8)
                    {
                        isbool = XChipUSB.UsbRegisterRead64(BinHelp.CurrentSize, ref pVule, true);
                        SerialSend.Sendser("R:0x" + (BinHelp.CurrentSize).ToString("X2"), pVule.ToString("X2"));
                    }
                    else
                    {
                        isbool = XChipUSB.UsbRegisterRead86(BinHelp.CurrentSize, ref pVule, true);
                        SerialSend.Sendser("R:0x" + (BinHelp.CurrentSize).ToString("X2"), pVule.ToString("X2"));
                    }
                    var chang = (int)(pVule >> 16); // 使用右移16位操作来获取高16位  
                    var kuang = (int)(pVule & 0xFFFF); // 使用位与操作来获取低16位  

                    BinHelp.SstBinUsbUpdateData(ref BinHelp.BinList, model.Address, add.ToString("X"), chang, kuang);
                }
            }
            else if (m_open)
            {

            }
            else
            {
                if (!sPortTemp.IsOpen && !m_open)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                    return;
                }
                
                if (null != BinHelp.lspList)
                {   //编写Bin更新数据
                    var temp = SerialSend.ReadRegister(BinHelp.BootSettingIdx, sPortTemp);
                    BinHelp.SetBinUpdateData(ref BinHelp.BinList, model.Address, add.ToString("X"), (uint)temp);
                }
            }
            SerialSend.WriteRegister(Convert.ToUInt32(model.Address, 16), add, sPortTemp);




        }
        private void panelControl6_Paint(object sender, PaintEventArgs e)
        {

        }

        private void AE_WIN_WIDTH_X_Leave(object sender, EventArgs e)
        {
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (isRead) return;
            ///三个文本框未空就不下发
            //if (string.IsNullOrEmpty(AE_WIDTH.Text) || string.IsNullOrEmpty(AE_WIN_WIDTH_X.Text) || string.IsNullOrEmpty(AE_WIN_WIDTH.Text)) return;

            ////获取文本框的值
            //long nWidth, nStartX, nAeWidth;
            //nWidth = long.Parse(AE_WIDTH.Text);
            //nStartX = long.Parse(AE_WIN_WIDTH_X.Text);
            //nAeWidth = long.Parse(AE_WIN_WIDTH.Text);
            //if (nStartX <= nWidth && nAeWidth <= nWidth && (nAeWidth + nStartX) <= nWidth)
            //{
            //    //判断//var nV = AE_radiomode.Properties.Items[AE_radiomode.SelectedIndex].Value;
            //    if (txtEdit.Tag.ToString() == "0")// nStartX写入
            //    {
            //        switch (AE_radiomode.SelectedIndex)
            //        {
            //            case 0:
            //                {
            //                    //备注  数据库里面没有  暂时放地址 // -by syd 20201014 数据库已增加，读数据还未替换成数据库中的东西
            //                    //SetAeRegister("AE_WIN_WIDTH_X_0_Day", (ulong)((nStartX >> 8) & 0xff));
            //                    //SetAeRegister("AE_WIN_WIDTH_X_1_Day", (ulong)(nStartX & 0xff));
            //                    //SetAeRegister("AE_WIN_WIDTH_X_0", (ulong)((nStartX >> 8) & 0xff));
            //                    //SetAeRegister("AE_WIN_WIDTH_X_1", (ulong)(nStartX & 0xff));
            //                    if (isWrite) return;
            //                    if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
            //                    {
            //                        if (isRead) return;
            //                        var txt = sender as DevExpress.XtraEditors.TextEdit;
            //                        if (string.IsNullOrEmpty(txt.Text)) return;
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_h"+ "_Day").ToLower()))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h" + "_Day").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_l" + "_Day").ToLower()))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l" + "_Day").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_h"))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_l"))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                    }
                               
            //                }
            //                break;
            //            case 1:
            //                {
            //                    //SetAeRegister("AE_WIN_WIDTH_X_0_Night", (ulong)((nStartX >> 8) & 0xff));
            //                    //SetAeRegister("AE_WIN_WIDTH_X_1_Night", (ulong)(nStartX & 0xff));
            //                    //SetAeRegister("AE_WIN_WIDTH_X_0", (ulong)((nStartX >> 8) & 0xff));
            //                    //SetAeRegister("AE_WIN_WIDTH_X_1", (ulong)(nStartX & 0xff));
            //                    if (isWrite) return;
            //                    if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
            //                    {
            //                        if (isRead) return;
            //                        var txt = sender as DevExpress.XtraEditors.TextEdit;
            //                        if (string.IsNullOrEmpty(txt.Text)) return;
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_h" + "_Night").ToLower()))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h" + "_Night").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_l" + "_Night").ToLower()))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l" + "_Night").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_h"))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_l"))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                    }
            //                }
            //                break;
            //            case 2:
            //                {
            //                    //SetAeRegister("AE_WIN_WIDTH_X_0_Midnight", (ulong)((nStartX >> 8) & 0xff));
            //                    //SetAeRegister("AE_WIN_WIDTH_X_1_Midnight", (ulong)(nStartX & 0xff));
            //                    //SetAeRegister("AE_WIN_WIDTH_X_0", (ulong)((nStartX >> 8) & 0xff));
            //                    //SetAeRegister("AE_WIN_WIDTH_X_1", (ulong)(nStartX & 0xff));
            //                    if (isWrite) return;
            //                    if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
            //                    {
            //                        if (isRead) return;
            //                        var txt = sender as DevExpress.XtraEditors.TextEdit;
            //                        if (string.IsNullOrEmpty(txt.Text)) return;
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_h" + "_Midnight").ToLower()))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h" + "_Midnight").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_l" + "_Midnight").ToLower()))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l" + "_Midnight").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_h"))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_l"))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                    }
            //                }
            //                break;
            //            default:
            //                {
            //                    //SetAeRegister("AE_WIN_WIDTH_X_0", (ulong)((nStartX >> 8) & 0xff));
            //                    //SetAeRegister("AE_WIN_WIDTH_X_1", (ulong)(nStartX & 0xff));
            //                    if (isWrite) return;
            //                    if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
            //                    {
            //                        if (isRead) return;
            //                        var txt = sender as DevExpress.XtraEditors.TextEdit;
            //                        if (string.IsNullOrEmpty(txt.Text)) return;
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_h"))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_l"))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                    }
            //                }
            //                break;
            //        }
            //    }
            //    else if (txtEdit.Tag.ToString() == "1")//nAeWidth写入
            //    {
            //        switch (AE_radiomode.SelectedIndex)
            //        {
            //            //case 0:
            //            //    {
            //            //        //备注  数据库里面没有  暂时放地址
            //            //        SetAeRegister("AE_WIN_WIDTH_0_Day", (ulong)((nAeWidth >> 8) & 0xff));
            //            //        SetAeRegister("AE_WIN_WIDTH_1_Day", (ulong)(nAeWidth & 0xff));
            //            //        SetAeRegister("AE_WIN_WIDTH_0", (ulong)((nAeWidth >> 8) & 0xff));
            //            //        SetAeRegister("AE_WIN_WIDTH_1", (ulong)(nAeWidth & 0xff));
            //            //    }
            //            //    break;
            //            //case 1:
            //            //    {
            //            //        SetAeRegister("AE_WIN_WIDTH_0_Night", (ulong)((nAeWidth >> 8) & 0xff));
            //            //        SetAeRegister("AE_WIN_WIDTH_1_Night", (ulong)(nAeWidth & 0xff));
            //            //        SetAeRegister("AE_WIN_WIDTH_0", (ulong)((nAeWidth >> 8) & 0xff));
            //            //        SetAeRegister("AE_WIN_WIDTH_1", (ulong)(nAeWidth & 0xff));
            //            //    }
            //            //    break;
            //            //case 2:
            //            //    {
            //            //        SetAeRegister("AE_WIN_WIDTH_0_Midnight", (ulong)((nAeWidth >> 8) & 0xff));
            //            //        SetAeRegister("AE_WIN_WIDTH_1_Midnight", (ulong)(nAeWidth & 0xff));
            //            //        SetAeRegister("AE_WIN_WIDTH_0", (ulong)((nAeWidth >> 8) & 0xff));
            //            //        SetAeRegister("AE_WIN_WIDTH_1", (ulong)(nAeWidth & 0xff));
            //            //    }
            //            //    break;
            //            //default:
            //            //    {
            //            //        SetAeRegister("AE_WIN_WIDTH_0", (ulong)((nAeWidth >> 8) & 0xff));
            //            //        SetAeRegister("AE_WIN_WIDTH_1", (ulong)(nAeWidth & 0xff));
            //            //    }
            //            //    break;
            //            case 0:
            //                {
                                
            //                    if (isWrite) return;
            //                    if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
            //                    {
            //                        if (isRead) return;
            //                        var txt = sender as DevExpress.XtraEditors.TextEdit;
            //                        if (string.IsNullOrEmpty(txt.Text)) return;
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_h" + "_Day").ToLower()))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h" + "_Day").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_l" + "_Day").ToLower()))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l" + "_Day").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_h"))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_l"))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                    }

            //                }
            //                break;
            //            case 1:
            //                {
                               
            //                    if (isWrite) return;
            //                    if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
            //                    {
            //                        if (isRead) return;
            //                        var txt = sender as DevExpress.XtraEditors.TextEdit;
            //                        if (string.IsNullOrEmpty(txt.Text)) return;
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_h" + "_Night").ToLower()))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h" + "_Night").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_l" + "_Night").ToLower()))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l" + "_Night").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_h"))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_l"))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                    }
            //                }
            //                break;
            //            case 2:
            //                {
                               
            //                    if (isWrite) return;
            //                    if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
            //                    {
            //                        if (isRead) return;
            //                        var txt = sender as DevExpress.XtraEditors.TextEdit;
            //                        if (string.IsNullOrEmpty(txt.Text)) return;
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_h" + "_Midnight").ToLower()))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h" + "_Midnight").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_l" + "_Midnight").ToLower()))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l" + "_Midnight").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_h"))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_l"))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                    }
            //                }
            //                break;
            //            default:
            //                {
                                
            //                    if (isWrite) return;
            //                    if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
            //                    {
            //                        if (isRead) return;
            //                        var txt = sender as DevExpress.XtraEditors.TextEdit;
            //                        if (string.IsNullOrEmpty(txt.Text)) return;
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_h"))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                        if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_l"))
            //                        {
            //                            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l").ToLower());
            //                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
            //                            SetAeRegister(model, Val);
            //                        }
            //                    }
            //                }
            //                break;
            //        }
            //    }

            //}
            //else
            //{
            //    DevExpress.XtraEditors.XtraMessageBox.Show("AE窗口范围超过图像大小,重新输入");
            //    return;
            //}
        }

        //private void AE_WIN_HEIGHT_Y_Leave(object sender, EventArgs e)
        //{
        //    var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
        //    if (isRead) return;
        //    if (string.IsNullOrEmpty(AE_HEIGHT.Text) || string.IsNullOrEmpty(AE_WIN_HEIGHT_Y.Text) || string.IsNullOrEmpty(AE_WIN_HEIGHT.Text)) return;
        //    long nHeight, nStartY, nAeHeight;
        //    nHeight = long.Parse(AE_HEIGHT.Text);
        //    nStartY = long.Parse(AE_WIN_HEIGHT_Y.Text);
        //    nAeHeight = long.Parse(AE_WIN_HEIGHT.Text);
        //    if (nStartY <= nHeight && nAeHeight <= nHeight && (nAeHeight + nStartY) <= nHeight)
        //    {
        //        if (txtEdit.Tag.ToString() == "0")// nStartY写入
        //        {
        //            switch (AE_radiomode.SelectedIndex)
        //            {
        //                //case 0:
        //                //    {
        //                //        //备注  数据库里面没有  暂时放地址
        //                //        SetAeRegister("AE_WIN_HEIGHT_Y_0_Day", (ulong)((nStartY >> 8) & 0xff));
        //                //        SetAeRegister("AE_WIN_HEIGHT_Y_1_Day", (ulong)(nStartY & 0xff));
        //                //        SetAeRegister("AE_WIN_HEIGHT_Y_0", (ulong)((nStartY >> 8) & 0xff));
        //                //        SetAeRegister("AE_WIN_HEIGHT_Y_1", (ulong)(nStartY & 0xff));
        //                //    }
        //                //    break;
        //                //case 1:
        //                //    {
        //                //        SetAeRegister("AE_WIN_HEIGHT_Y_0_Night", (ulong)((nStartY >> 8) & 0xff));
        //                //        SetAeRegister("AE_WIN_HEIGHT_Y_1_Night", (ulong)(nStartY & 0xff));
        //                //        SetAeRegister("AE_WIN_HEIGHT_Y_0", (ulong)((nStartY >> 8) & 0xff));
        //                //        SetAeRegister("AE_WIN_HEIGHT_Y_1", (ulong)(nStartY & 0xff));
        //                //    }
        //                //    break;
        //                //case 2:
        //                //    {
        //                //        SetAeRegister("AE_WIN_HEIGHT_Y_0_Midnight", (ulong)((nStartY >> 8) & 0xff));
        //                //        SetAeRegister("AE_WIN_HEIGHT_Y_1_Midnight", (ulong)(nStartY & 0xff));
        //                //        SetAeRegister("AE_WIN_HEIGHT_Y_0", (ulong)((nStartY >> 8) & 0xff));
        //                //        SetAeRegister("AE_WIN_HEIGHT_Y_1", (ulong)(nStartY & 0xff));
        //                //    }
        //                //    break;
        //                //default:
        //                //    {
        //                //        SetAeRegister("AE_WIN_HEIGHT_Y_0", (ulong)((nStartY >> 8) & 0xff));
        //                //        SetAeRegister("AE_WIN_HEIGHT_Y_1", (ulong)(nStartY & 0xff));
        //                //    }
        //                //    break;
        //                case 0:
        //                    {

        //                        if (isWrite) return;
        //                        if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
        //                        {
        //                            if (isRead) return;
        //                            var txt = sender as DevExpress.XtraEditors.TextEdit;
        //                            if (string.IsNullOrEmpty(txt.Text)) return;
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_h" + "_Day").ToLower()))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h" + "_Day").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_l" + "_Day").ToLower()))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l" + "_Day").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_h"))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_l"))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                        }

        //                    }
        //                    break;
        //                case 1:
        //                    {

        //                        if (isWrite) return;
        //                        if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
        //                        {
        //                            if (isRead) return;
        //                            var txt = sender as DevExpress.XtraEditors.TextEdit;
        //                            if (string.IsNullOrEmpty(txt.Text)) return;
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_h" + "_Night").ToLower()))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h" + "_Night").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_l" + "_Night").ToLower()))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l" + "_Night").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_h"))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_l"))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                        }
        //                    }
        //                    break;
        //                case 2:
        //                    {

        //                        if (isWrite) return;
        //                        if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
        //                        {
        //                            if (isRead) return;
        //                            var txt = sender as DevExpress.XtraEditors.TextEdit;
        //                            if (string.IsNullOrEmpty(txt.Text)) return;
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_h" + "_Midnight").ToLower()))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h" + "_Midnight").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_l" + "_Midnight").ToLower()))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l" + "_Midnight").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_h"))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_l"))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                        }
        //                    }
        //                    break;
        //                default:
        //                    {

        //                        if (isWrite) return;
        //                        if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
        //                        {
        //                            if (isRead) return;
        //                            var txt = sender as DevExpress.XtraEditors.TextEdit;
        //                            if (string.IsNullOrEmpty(txt.Text)) return;
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_h"))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_l"))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                        }
        //                    }
        //                    break;
        //            }
        //        }
        //        else if (txtEdit.Tag.ToString() == "1")//nAeHeight写入
        //        {
        //            switch (AE_radiomode.SelectedIndex)
        //            {
        //                //case 0:
        //                //    {
        //                //        //备注  数据库里面没有  暂时放地址
        //                //        SetAeRegister("AE_WIN_HEIGHT_0_Day", (ulong)((nAeHeight >> 8) & 0xff));
        //                //        SetAeRegister("AE_WIN_HEIGHT_1_Day", (ulong)(nAeHeight & 0xff));
        //                //        SetAeRegister("AE_WIN_HEIGHT_0", (ulong)((nAeHeight >> 8) & 0xff));
        //                //        SetAeRegister("AE_WIN_HEIGHT_1", (ulong)(nAeHeight & 0xff));
        //                //    }
        //                //    break;
        //                //case 1:
        //                //    {
        //                //        SetAeRegister("AE_WIN_HEIGHT_0_Night", (ulong)((nAeHeight >> 8) & 0xff));
        //                //        SetAeRegister("AE_WIN_HEIGHT_1_Night", (ulong)(nAeHeight & 0xff));
        //                //        SetAeRegister("AE_WIN_HEIGHT_0", (ulong)((nAeHeight >> 8) & 0xff));
        //                //        SetAeRegister("AE_WIN_HEIGHT_1", (ulong)(nAeHeight & 0xff));
        //                //    }
        //                //    break;
        //                //case 2:
        //                //    {
        //                //        SetAeRegister("AE_WIN_HEIGHT_0_Midnight", (ulong)((nAeHeight >> 8) & 0xff));
        //                //        SetAeRegister("AE_WIN_HEIGHT_1_Midnight", (ulong)(nAeHeight & 0xff));
        //                //        SetAeRegister("AE_WIN_HEIGHT_0", (ulong)((nAeHeight >> 8) & 0xff));
        //                //        SetAeRegister("AE_WIN_HEIGHT_1", (ulong)(nAeHeight & 0xff));
        //                //    }
        //                //    break;
        //                //default:
        //                //    {
        //                //        SetAeRegister("AE_WIN_HEIGHT_0", (ulong)((nAeHeight >> 8) & 0xff));
        //                //        SetAeRegister("AE_WIN_HEIGHT_1", (ulong)(nAeHeight & 0xff));
        //                //    }
        //                //    break;
        //                case 0:
        //                    {

        //                        if (isWrite) return;
        //                        if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
        //                        {
        //                            if (isRead) return;
        //                            var txt = sender as DevExpress.XtraEditors.TextEdit;
        //                            if (string.IsNullOrEmpty(txt.Text)) return;
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_h" + "_Day").ToLower()))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h" + "_Day").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_l" + "_Day").ToLower()))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l" + "_Day").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_h"))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_l"))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                        }

        //                    }
        //                    break;
        //                case 1:
        //                    {

        //                        if (isWrite) return;
        //                        if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
        //                        {
        //                            if (isRead) return;
        //                            var txt = sender as DevExpress.XtraEditors.TextEdit;
        //                            if (string.IsNullOrEmpty(txt.Text)) return;
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_h" + "_Night").ToLower()))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h" + "_Night").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_l" + "_Night").ToLower()))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l" + "_Night").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_h"))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_l"))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                        }
        //                    }
        //                    break;
        //                case 2:
        //                    {

        //                        if (isWrite) return;
        //                        if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
        //                        {
        //                            if (isRead) return;
        //                            var txt = sender as DevExpress.XtraEditors.TextEdit;
        //                            if (string.IsNullOrEmpty(txt.Text)) return;
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_h" + "_Midnight").ToLower()))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h" + "_Midnight").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_l" + "_Midnight").ToLower()))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l" + "_Midnight").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_h"))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_l"))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                        }
        //                    }
        //                    break;
        //                default:
        //                    {

        //                        if (isWrite) return;
        //                        if (true)
        //                        {
        //                            if (isRead) return;
        //                            var txt = sender as DevExpress.XtraEditors.TextEdit;
        //                            if (string.IsNullOrEmpty(txt.Text)) return;
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_h"))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                            if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_l"))
        //                            {
        //                                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l").ToLower());
        //                                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Convert.ToString(int.Parse(txt.Text), 16));
        //                                SetAeRegister(model, Val);
        //                            }
        //                        }
        //                    }
        //                    break;
        //            }
        //        }
        //    }
        //    else
        //    {
        //        DevExpress.XtraEditors.XtraMessageBox.Show("AE窗口范围超过图像大小,重新输入");
        //        return;
        //    }
        //}

        //private void AE_WEIGHT1_Leave(object sender, EventArgs e)
        //{
        //    if (isWrite) return;
        //    if (isRead) return;
        //    var txtedit = sender as DevExpress.XtraEditors.TextEdit;
        //    if (string.IsNullOrEmpty(txtedit.Text)) return;
        //    for (int i = 0; i < 64; i++)
        //    {
        //        foreach (Control item in panelControl5.Controls)
        //        {
        //            if ($"AE_WEIGHT_{(i + 1).ToString("D2")}" == item.Name && (txtedit.Name == item.Name))
        //            {
        //                var TempTxt = item.Text;
        //                if (Convert.ToInt32(item.Text, 16) > 0x40)
        //                {
        //                    TempTxt = "40";
        //                }
        //                switch (AE_radiomode.SelectedIndex)
        //                {
        //                    case 0:
        //                        { //day mode
        //                            var uValue = GetAERegister($"AE_WEIGHT_{(i + 1).ToString("D2")}_Day");
        //                            if ((uValue >> 7) == 1)
        //                            {
        //                                SetAeRegister($"AE_WEIGHT_{(i + 1).ToString("D2")}_Day", (Convert.ToUInt32(TempTxt, 16) | 0x80));

        //                            }
        //                            else
        //                            {
        //                                SetAeRegister($"AE_WEIGHT_{(i + 1).ToString("D2")}_Day", (Convert.ToUInt32(TempTxt, 16) & 0x7f));

        //                            }
        //                        }
        //                        break;
        //                    case 1:
        //                        { //night mode
        //                            var uValue = GetAERegister($"AE_WEIGHT_{(i + 1).ToString("D2")}_Night");
        //                            if ((uValue >> 7) == 1)
        //                            {
        //                                SetAeRegister($"AE_WEIGHT_{(i + 1).ToString("D2")}_Night", (Convert.ToUInt32(TempTxt, 16) | 0x80));

        //                            }
        //                            else
        //                            {
        //                                SetAeRegister($"AE_WEIGHT_{(i + 1).ToString("D2")}_Night", (Convert.ToUInt32(TempTxt, 16) & 0x7f));

        //                            }
        //                        }
        //                        break;
        //                    case 2:
        //                        {
        //                            //midnight mode
        //                            var uValue = GetAERegister($"AE_WEIGHT_{(i + 1).ToString("D2")}_Midnight");
        //                            if ((uValue >> 7) == 1)
        //                            {
        //                                SetAeRegister($"AE_WEIGHT_{(i + 1).ToString("D2")}_Midnight", (Convert.ToUInt32(TempTxt, 16) | 0x80));

        //                            }
        //                            else
        //                            {
        //                                SetAeRegister($"AE_WEIGHT_{(i + 1).ToString("D2")}_Midnight", (Convert.ToUInt32(TempTxt, 16) & 0x7f));

        //                            }
        //                        }
        //                        break;
        //                    default:
        //                        break;
        //                }

        //                var nTemp = GetAERegister($"AE_WEIGHT_{(i + 1).ToString("D2")}");//SerialSend.ReadRegister((ulong)( | (0x5B + i)), sPortTemp);
        //                if ((nTemp >> 7) == 1)
        //                {
        //                    SetAeRegister($"AE_WEIGHT_{(i + 1).ToString("D2")}", (Convert.ToUInt32(TempTxt, 16) | 0x80)); //SerialSend.WriteRegister((ulong)( | (0x5B + i)), (ulong)(Convert.ToInt32(TempTxt, 16) | 0x80), sPortTemp);
        //                }
        //                else
        //                {
        //                    SetAeRegister($"AE_WEIGHT_{(i + 1).ToString("D2")}", (Convert.ToUInt32(TempTxt, 16) & 0x7f));// SerialSend.WriteRegister((ulong)( | (0x5B + i)), (ulong)(Convert.ToInt32(TempTxt, 16) & 0x7f), sPortTemp);
        //                }
        //                //重新赋值
        //                setLabTxtJiu(i, nTemp.ToString("X2"));
        //                if (Convert.ToInt32(item.Text, 16) > 0x40)
        //                {
        //                    isRead = true;
        //                    item.Text = "40";
        //                    isRead = false;
        //                    return;
        //                }
        //                //setLabTxtJiu(txtedit.Tag, txtedit.Text);
        //            }
        //        }
        //    }
        //    //setLabTxtJiu(txtedit.Tag, txtedit.Text);
        //}

        private void AE_BRIGHTNESS_Leave(object sender, EventArgs e)
        {
            //if (isWrite) return;
            //if (isRead) return;
            //var txtedit = sender as DevExpress.XtraEditors.TextEdit;
            //if (string.IsNullOrEmpty(txtedit.Text)) return;
            ////注： 这个新写的 数据库中没有
            //SetAeRegister("AE_BRIGHTNESS0", (((ulong)Convert.ToInt32(txtedit.Text, 16) >> 8) & 0xff));
            //SetAeRegister("AE_BRIGHTNESS1", ((ulong)Convert.ToInt32(txtedit.Text, 16) & 0xff));

            if (isWrite) return;
            
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_h"))
                {
                    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h").ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetAeRegister(model, Val);
                }
                if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_l"))
                {
                    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l").ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetAeRegister(model, Val);
                }
            
        }

        private void AE_MAXGAIN_Leave(object sender, EventArgs e)
        {
            //if (isWrite) return;
            //if (isRead) return;
            //var txtedit = sender as DevExpress.XtraEditors.TextEdit;
            //if (string.IsNullOrEmpty(txtedit.Text)) return;
            //注： 这个新写的 数据库中没有
            if (Convert.ToInt32(AE_MAXGAIN.Text, 16) >= 0xffff || Convert.ToInt32(AE_MAXGAIN.Text, 16) < 0)
            {
                if (Convert.ToInt32(AE_MAXGAIN.Text, 16) == 0xffff) return;
                AE_MAXGAIN.Text = "ffff"; return;
            }
            //SetAeRegister("AE_MAXGAIN0", (((ulong)Convert.ToInt32(txtedit.Text, 16) >> 8) & 0xff));
            //SetAeRegister("AE_MAXGAIN1", ((ulong)Convert.ToInt32(txtedit.Text, 16) & 0xff));
            if (isWrite) return;
            
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_h"))
                {
                    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h").ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetAeRegister(model, Val);
                }
                if (XChipAe.Exists(c => c.NameType.ToLower() == txt.Name.ToLower() + "_l"))
                {
                    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l").ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetAeRegister(model, Val);
                }
            
        }

        private void AE_MINEXPOSURE_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            var txtedit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtedit.Text)) return;
            //注： 这个新写的 数据库中没有 , 
            if (Convert.ToInt32(AE_MINEXPOSURE.Text, 16) >= 0xffff || Convert.ToInt32(AE_MINEXPOSURE.Text, 16) < 0)
            {
                if (Convert.ToInt32(AE_MINEXPOSURE.Text, 16) == 0xffff) return;
                AE_MINEXPOSURE.Text = "ffff"; return;
            }
            if (Convert.ToInt32(AE_MINEXPOSURE.Text, 16) <= Convert.ToInt32(AE_MAXEXPOSURE.Text, 16))
            {
                //SetAeRegister("AE_MINEXPOSURE0", (((ulong)Convert.ToInt32(txtedit.Text, 16) >> 8) & 0xff));
                //SetAeRegister("AE_MINEXPOSURE1", ((ulong)Convert.ToInt32(txtedit.Text, 16) & 0xf0));
                if (isWrite) return;
                
                    if (isRead) return;
                    if (string.IsNullOrEmpty(txtedit.Text)) return;
                    if (XChipAe.Exists(c => c.NameType.ToLower() == txtedit.Name.ToLower() + "_h"))
                    {
                        var model = XChipAe.Find(c => c.NameType.ToLower() == (txtedit.Name + "_h").ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtedit.Text);
                        SetAeRegister(model, Val);
                    }
                    if (XChipAe.Exists(c => c.NameType.ToLower() == txtedit.Name.ToLower() + "_l"))
                    {
                        var model = XChipAe.Find(c => c.NameType.ToLower() == (txtedit.Name + "_l").ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtedit.Text);
                        SetAeRegister(model, Val);
                    }
                
            }
            else
            {
                //SetAeRegister("AE_MINEXPOSURE0", (((ulong)Convert.ToInt32(AE_MAXEXPOSURE.Text, 16) >> 8) & 0xff));
                //SetAeRegister("AE_MINEXPOSURE1", ((ulong)Convert.ToInt32(AE_MAXEXPOSURE.Text, 16) & 0xf0));
                if (isWrite) return;
                
                    if (isRead) return;
                    if (string.IsNullOrEmpty(txtedit.Text)) return;
                    if (XChipAe.Exists(c => c.NameType.ToLower() == txtedit.Name.ToLower() + "_h"))
                    {
                        var model = XChipAe.Find(c => c.NameType.ToLower() == (txtedit.Name + "_h").ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, AE_MAXEXPOSURE.Text);
                        SetAeRegister(model, Val);
                    }
                    if (XChipAe.Exists(c => c.NameType.ToLower() == txtedit.Name.ToLower() + "_l"))
                    {
                        var model = XChipAe.Find(c => c.NameType.ToLower() == (txtedit.Name + "_l").ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, AE_MAXEXPOSURE.Text);
                        SetAeRegister(model, Val);
                    }
                
            }
        }

        private void AE_MAXEXPOSURE_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            var txtedit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtedit.Text)) return;
            var nValue = (Convert.ToUInt32(txtedit.Text, 16) << 4);
            //if (GetCheckListBox(checkedListBoxControl2) == "0") //50hz
            //{

            //    var setFlick50 = GetAERegister("SetFlick50_h") << 8 | GetAERegister("SetFlick50_l");
            //    var temp = (int)((float)nValue / (float)setFlick50 + 0.5) * setFlick50;
            //    SetAeRegister("AE_MAXEXPOSURE_h", (ulong)(temp >> 24));
            //    SetAeRegister("AE_MAXEXPOSURE_m1",(ulong)(temp >> 16 & 0xff));
            //    SetAeRegister("AE_MAXEXPOSURE_m0",(ulong)(temp >> 8 & 0xff));
            //    SetAeRegister("AE_MAXEXPOSURE_l", (ulong)(temp & 0xf0));
            //}
            //else if (GetCheckListBox(checkedListBoxControl2) == "1") //60hz
            //{
                
            //    var setFlick60 = GetAERegister("SetFlick60_h") << 8 | GetAERegister("SetFlick60_l");
            //    var temp = (int)((float)nValue / (float)setFlick60 + 0.5) * setFlick60;
            //    SetAeRegister("AE_MAXEXPOSURE_h", (ulong)(temp >> 24));
            //    SetAeRegister("AE_MAXEXPOSURE_m1", (ulong)(temp >> 16 & 0xff));
            //    SetAeRegister("AE_MAXEXPOSURE_m0", (ulong)(temp >> 8 & 0xff));
            //    SetAeRegister("AE_MAXEXPOSURE_l", (ulong)(temp & 0xf0));
            //}
            //else
            //{
            //    SetAeRegister("AE_MAXEXPOSURE_h",  (ulong)(Convert.ToInt32(txtedit.Text, 16) >> 24));
            //    SetAeRegister("AE_MAXEXPOSURE_m1", (ulong)(Convert.ToInt32(txtedit.Text, 16) >> 16 & 0xff));
            //    SetAeRegister("AE_MAXEXPOSURE_m0", (ulong)(Convert.ToInt32(txtedit.Text, 16) >> 8 & 0xff));
            //    SetAeRegister("AE_MAXEXPOSURE_l", (ulong)(Convert.ToInt32(txtedit.Text, 16) & 0xf0));
            //}
            SetAeRegister("AE_MAXEXPOSURE_h", (ulong)(Convert.ToInt32(txtedit.Text, 16) >> 24));
            SetAeRegister("AE_MAXEXPOSURE_m1", (ulong)(Convert.ToInt32(txtedit.Text, 16) >> 16 & 0xff));
            SetAeRegister("AE_MAXEXPOSURE_m0", (ulong)(Convert.ToInt32(txtedit.Text, 16) >> 8 & 0xff));
            SetAeRegister("AE_MAXEXPOSURE_l", (ulong)(Convert.ToInt32(txtedit.Text, 16) & 0xf0));
        }

        private void AE_TARGET_JUDGE_TAB0_Leave(object sender, EventArgs e)
        {
            //if (isWrite) return;
            //var txtedit = sender as DevExpress.XtraEditors.TextEdit;
            //if (string.IsNullOrEmpty(txtedit.Text)) return;
            //int i = Convert.ToInt16(txtedit.Tag);

            //uint high = Convert.ToUInt32(txtedit.Text, 16) >> 8;
            //uint low = Convert.ToUInt32(txtedit.Text, 16) & 0xff;
            //SetAeRegister($"AE_TARGET_JUDGE_TAB{2 * i}_Day", high);
            //SetAeRegister($"AE_TARGET_JUDGE_TAB{2 * i + 1}_Day", low);
            //SetAeRegister($"AE_TARGET_JUDGE_TAB{2 * i}_Night", high);
            //SetAeRegister($"AE_TARGET_JUDGE_TAB{2 * i + 1}_Night", low);

            //SetAeRegister($"AE_TARGET_JUDGE_TAB{2 * i}_Day_Set", high);
            //SetAeRegister($"AE_TARGET_JUDGE_TAB{2 * i + 1}_Day_Set", low);
            //SetAeRegister($"AE_TARGET_JUDGE_TAB{2 * i}_Night_Set", high);
            //SetAeRegister($"AE_TARGET_JUDGE_TAB{2 * i + 1}_Night_Set", low);
            if (isWrite) return;
            
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                //List<string> modelist = new List<string>() { "_Day", "_Night", "_Day_Set", "_Night_Set" };
                //foreach (var item in modelist)
                //{
                //    if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_h" + item).ToLower()))
                //    {
                //        var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h" + item).ToLower());
                //        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                //        SetAeRegister(model, Val);
                //    }
                //    if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_m1" + item).ToLower()))
                //    {
                //        var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_m1" + item).ToLower());
                //        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                //        SetAeRegister(model, Val);
                //    }
                //    if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_m0" + item).ToLower()))
                //    {
                //        var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_0m" + item).ToLower());
                //        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                //        SetAeRegister(model, Val);
                //    }
                //    if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_l" + item).ToLower()))
                //    {
                //        var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l" + item).ToLower());
                //        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                //        SetAeRegister(model, Val);
                //    }
                //}
            //if (Mode_switch.EditValue.ToString() == "1")
            //{
                var item = "_Day";
                if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_h" + item).ToLower()))
                {
                    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h" + item).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetAeRegister(model, Val);
                }
                if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_m1" + item).ToLower()))
                {
                    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_m1" + item).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetAeRegister(model, Val);
                }
                if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_m0" + item).ToLower()))
                {
                    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_0m" + item).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetAeRegister(model, Val);
                }
                if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_l" + item).ToLower()))
                {
                    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l" + item).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetAeRegister(model, Val);
                }
            //}
            //else
            //{
            //item = "_Night";
            //if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_h" + item).ToLower()))
            //{
            //    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h" + item).ToLower());
            //    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
            //    SetAeRegister(model, Val);
            //}
            //if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_m1" + item).ToLower()))
            //{
            //    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_m1" + item).ToLower());
            //    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
            //    SetAeRegister(model, Val);
            //}
            //if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_m0" + item).ToLower()))
            //{
            //    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_0m" + item).ToLower());
            //    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
            //    SetAeRegister(model, Val);
            //}
            //if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_l" + item).ToLower()))
            //{
            //    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l" + item).ToLower());
            //    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
            //    SetAeRegister(model, Val);
            //}
            //}
            var OsdDateExist = SerialSend.ReadRegister(0x200011f3, sPortTemp);
            if (GetConfigValue("IsSerial") == "USB" || GetConfigValue("IsI2C") == "1" || OsdDateExist == 0x1)
            {
                item = "_Day_Set";
                if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_h" + item).ToLower()))
                {
                    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h" + item).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetAeRegister(model, Val);
                }
                if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_m1" + item).ToLower()))
                {
                    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_m1" + item).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetAeRegister(model, Val);
                }
                if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_m0" + item).ToLower()))
                {
                    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_0m" + item).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetAeRegister(model, Val);
                }
                if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_l" + item).ToLower()))
                {
                    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l" + item).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetAeRegister(model, Val);
                }

                //item = "_Night_Set";
                //if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_h" + item).ToLower()))
                //{
                //    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h" + item).ToLower());
                //    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                //    SetAeRegister(model, Val);
                //}
                //if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_m1" + item).ToLower()))
                //{
                //    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_m1" + item).ToLower());
                //    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                //    SetAeRegister(model, Val);
                //}
                //if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_m0" + item).ToLower()))
                //{
                //    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_0m" + item).ToLower());
                //    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                //    SetAeRegister(model, Val);
                //}
                //if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_l" + item).ToLower()))
                //{
                //    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l" + item).ToLower());
                //    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                //    SetAeRegister(model, Val);
                //}
            }

        }
        private string GetConfigValue(string strKey)
        {
            var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var settings = configFile.AppSettings.Settings;

            return settings[strKey].Value;
        }
        private void AE_TARGET_JUDGE_THR0_Leave(object sender, EventArgs e)
        {
            //if (Mono_mode_en.Enabled)
            //{
            //    if (isWrite) return;
                
            //        if (isRead) return;
            //        var txt = sender as DevExpress.XtraEditors.TextEdit;
            //        if (string.IsNullOrEmpty(txt.Text)) return;
            //        string scenesMask = Mono_mode_en.Checked ? "_Night" : "_Day";
            //        if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_h" + scenesMask).ToLower()))
            //        {
            //            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h" + scenesMask).ToLower());
            //            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
            //            SetAeRegister(model, Val);
            //        }
            //        if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_m1" + scenesMask).ToLower()))
            //        {
            //            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_m1" + scenesMask).ToLower());
            //            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
            //            SetAeRegister(model, Val);
            //        }
            //        if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_m0" + scenesMask).ToLower()))
            //        {
            //            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_m0" + scenesMask).ToLower());
            //            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
            //            SetAeRegister(model, Val);
            //        }
            //        if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_l" + scenesMask).ToLower()))
            //        {
            //            var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l" + scenesMask).ToLower());
            //            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
            //            SetAeRegister(model, Val);
            //        }
                    

               
            //}
            //else
            //{
            //    if (isWrite) return;
                
            //        if (isRead) return;
            //        var txt = sender as DevExpress.XtraEditors.TextEdit;
            //        if (string.IsNullOrEmpty(txt.Text)) return;
            //        List<string> modelist = new List<string>() { "_Day", "_Night" };
            //        foreach (var item in modelist)
            //        {
            //            if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_h" + item).ToLower()))
            //            {
            //                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h" + item).ToLower());
            //                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
            //                SetAeRegister(model, Val);
            //            }
            //            if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_m1" + item).ToLower()))
            //            {
            //                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_m1" + item).ToLower());
            //                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
            //                SetAeRegister(model, Val);
            //            }
            //            if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_m0" + item).ToLower()))
            //            {
            //                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_m0" + item).ToLower());
            //                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
            //                SetAeRegister(model, Val);
            //            }
            //            if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_l" + item).ToLower()))
            //            {
            //                var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l" + item).ToLower());
            //                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
            //                SetAeRegister(model, Val);
            //            }
            //        }               
            //}

            if (isWrite) return;

            if (isRead) return;
            var txt = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txt.Text)) return;

            //if (Mode_switch.EditValue.ToString() == "1")
            //{
                var item = "_Day";
                if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_h" + item).ToLower()))
                {
                    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h" + item).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetAeRegister(model, Val);
                }
                if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_m1" + item).ToLower()))
                {
                    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_m1" + item).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetAeRegister(model, Val);
                }
                if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_m0" + item).ToLower()))
                {
                    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_m0" + item).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetAeRegister(model, Val);
                }
                if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_l" + item).ToLower()))
                {
                    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l" + item).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetAeRegister(model, Val);
                }
            //}
            //else
            //{
                //item = "_Night";
                //if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_h" + item).ToLower()))
                //{
                //    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h" + item).ToLower());
                //    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                //    SetAeRegister(model, Val);
                //}
                //if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_m1" + item).ToLower()))
                //{
                //    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_m1" + item).ToLower());
                //    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                //    SetAeRegister(model, Val);
                //}
                //if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_m0" + item).ToLower()))
                //{
                //    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_m0" + item).ToLower());
                //    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                //    SetAeRegister(model, Val);
                //}
                //if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_l" + item).ToLower()))
                //{
                //    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l" + item).ToLower());
                //    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                //    SetAeRegister(model, Val);
                //}
            //}
        }

        private void Factor_Leave(object sender, EventArgs e)
        {
            //if (isWrite) return;
            //if (isRead) return;
            //if (string.IsNullOrEmpty(Factor.Text)) return;
            //var nTemp = Convert.ToUInt32(Factor.Text, 16);
            //var nBHigh = nTemp >> 24;
            //var nHigh = nTemp >> 16 & 0xff;
            //var nMid = nTemp >> 8 & 0xff;
            //var nLow = nTemp & 0xff;
            //SetAeRegister("Factor_h", (ulong)nBHigh);
            //SetAeRegister("Factor_m0", (ulong)nHigh);
            //SetAeRegister("Factor_m1", (ulong)nMid);
            //SetAeRegister("Factor_l", (ulong)nLow);
            if (isWrite) return;
            
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                //string scenesMask = Mono_mode_en.Checked ? "_Night" : "_Day";
                if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_h").ToLower()))
                {
                    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_h").ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetAeRegister(model, Val);
                }
                if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_m1" ).ToLower()))
                {
                    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_m1" ).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetAeRegister(model, Val);
                }
                if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_m0" ).ToLower()))
                {
                    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_m0").ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetAeRegister(model, Val);
                }
                if (XChipAe.Exists(c => c.NameType.ToLower() == (txt.Name + "_l" ).ToLower()))
                {
                    var model = XChipAe.Find(c => c.NameType.ToLower() == (txt.Name + "_l" ).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetAeRegister(model, Val);
                }


            
        }

        private void AE_SENSORMAXGAIN_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return;
            var txtedit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtedit.Text)) return;
            //注： 这个新写的 数据库中没有
            if (Convert.ToInt32(txtedit.Text, 16) >= 0xffff || Convert.ToInt32(txtedit.Text, 16) < 0)
            {
                if (Convert.ToInt32(txtedit.Text, 16) == 0xffff) return;
                AE_SENSORMAXGAIN.Text = "ffff"; return;
            }
            if (Convert.ToInt32(AE_SENSORMAXGAIN.Text, 16) <= Convert.ToInt32(AE_MAXGAIN.Text, 16))
            {
                //SetAeRegister("AE_SENSORMAXGAIN0", (((ulong)Convert.ToInt32(txtedit.Text, 16) >> 8) & 0xff));
                //SetAeRegister("AE_SENSORMAXGAIN1", ((ulong)Convert.ToInt32(txtedit.Text, 16) & 0xff));
                if (isWrite) return;
                
                    if (isRead) return;
                    if (string.IsNullOrEmpty(txtedit.Text)) return;
                    if (XChipAe.Exists(c => c.NameType.ToLower() == txtedit.Name.ToLower() + "_h"))
                    {
                        var model = XChipAe.Find(c => c.NameType.ToLower() == (txtedit.Name + "_h").ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtedit.Text);
                        SetAeRegister(model, Val);
                    }
                    if (XChipAe.Exists(c => c.NameType.ToLower() == txtedit.Name.ToLower() + "_l"))
                    {
                        var model = XChipAe.Find(c => c.NameType.ToLower() == (txtedit.Name + "_l").ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtedit.Text);
                        SetAeRegister(model, Val);
                    }
                
            }
            else
            {
                //SetAeRegister("AE_SENSORMAXGAIN0", (((ulong)Convert.ToInt32(AE_MAXGAIN.Text, 16) >> 8) & 0xff));
                //SetAeRegister("AE_SENSORMAXGAIN1", ((ulong)Convert.ToInt32(AE_MAXGAIN.Text, 16) & 0xff));
                if (isWrite) return;
                
                    if (isRead) return;
                    if (string.IsNullOrEmpty(txtedit.Text)) return;
                    if (XChipAe.Exists(c => c.NameType.ToLower() == txtedit.Name.ToLower() + "_h"))
                    {
                        var model = XChipAe.Find(c => c.NameType.ToLower() == (txtedit.Name + "_h").ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, AE_MAXGAIN.Text);
                        SetAeRegister(model, Val);
                    }
                    if (XChipAe.Exists(c => c.NameType.ToLower() == txtedit.Name.ToLower() + "_l"))
                    {
                        var model = XChipAe.Find(c => c.NameType.ToLower() == (txtedit.Name + "_l").ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, AE_MAXGAIN.Text);
                        SetAeRegister(model, Val);
                    }
                
            }
        }

        private void AE_WIN_WIDTH_X_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                AE_WIN_WIDTH_X_Leave(sender, null);
            }
        }

        private void AE_WIN_HEIGHT_Y_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //AE_WIN_HEIGHT_Y_Leave(sender, null);
            }
        }

        private void AE_BRIGHTNESS_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                AE_BRIGHTNESS_Leave(sender, null);
            }
        }

        private void AE_MAXGAIN_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                AE_MAXGAIN_Leave(sender, null);
            }
        }

        private void AE_MINEXPOSURE_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                AE_MINEXPOSURE_Leave(sender, null);
            }
        }

        private void AE_MAXEXPOSURE_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                AE_MAXEXPOSURE_Leave(sender, null);
            }
        }

        private void AE_SENSORMAXGAIN_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                AE_SENSORMAXGAIN_Leave(sender, null);
            }
        }

        private void AE_TARGET_JUDGE_TAB0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                AE_TARGET_JUDGE_TAB0_Leave(sender, null);
            }
        }

        private void Factor_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Factor_Leave(sender, null);
            }
        }

        private void AE_TARGET_JUDGE_THR0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                AE_TARGET_JUDGE_THR0_Leave(sender, null);
            }
        }

        private void simpleButton6_Click(object sender, EventArgs e)
        {
            //string str = "       设置有效的AE统计窗口，将曝光统计窗口划分为5x5的亮度统计块，可设置每块的权重值，取值范围为0x00~0x40，其值越大对计算平均亮度的影响就会越大；也可勾选“关注区域使能”设置是否将亮度统计块作为关注区域，根据不同的产品运用设置不同关注区域可用于控制收光；权重和关注区域设置后需点击“Refresh”后才能生效。\r\n       Scene_Switch使能后，勾选“调试”可分别调整Day_mode、night_mode、midnight_mode场景下的AE统计窗口和权重值。\r\n       Target_Switch_Mode可通过计算当前Fincal_cal值同Judge_thr设定值进行比较，将对应的Table值映射至“目标亮度”寄存器地址中，其中：\r\n       if (Fincal_cal < Judge_thr[0]) →{Table[0]}\r\n       else if (Fincal_cal < Judge_thr[1]) →{Table[1]}\r\n       else if (Fincal_cal < Judge_thr[2]) →{Table[2]}\r\n       else if (Fincal_cal < Judge_thr[3]) →{Table[3]}\r\n       else if (Fincal_cal < Judge_thr[4]) →{Table[4]}\r\n       else →{Table[5]}\r\n       更多详见《调试工具用户手册》。\r\n";
            //XChip_OptimizationTips ot = new XChip_OptimizationTips("AE调优Tips",str);           
            //ot.StartPosition = FormStartPosition.CenterParent;
            //Task.Run(() =>
            //{
            //    ot.ShowDialog();
            //});
            Thread thread = new Thread(threadPro);
            thread.Start();

        }

        private void threadPro()

        {

            MethodInvoker methodInvoker = new MethodInvoker(ShowFormB);

            BeginInvoke(methodInvoker);

        }

        private void ShowFormB()

        {
            //string str = "\t      \t 设置有效的AE统计窗口，将曝光统计窗口划分为5x5的亮度统计块，可勾选“Area_weight”设置每区域块的权重值，取值范围为0x00~0x40，其值越大对计算平均亮度的影响就会越大，权重值设置后需点击“Refresh”后才能生效。\r\n"
            //   + "\t      \tAvgCal模块在重新触发AE机制中会将64个区域块平均亮度进行由低至高排序，start_blk设定纳入测光统计的起始亮度块，blk_num设定纳入测光统计的亮度块数目，注意：start_blk的值+blk_num的值需≤0x19；且该模块使能与“关注区域”模块使能互斥。\r\n "
            //   + "\t      \t Scene_Switch使能后，勾选“Debug”可分别调整Day_mode、night_mode、midnight_mode场景下的AE统计窗口和权重值。\r\n"
            //   + "\t      \t LFC_Ctrl使能后，可调整“insert_frame_num_max”进行插帧设定，0x10表示插入一帧，0x20表示插入2帧...以此类推。\r\n"
            //   + "\t      \t Target_Judge_Table可通过计算当前Final_cal值同Judge_thr设定值进行比较，将对应的Table值映射至“AE_target”寄存器地址中，其中：\r\n"
            //   + "\t      \t if (Fincal_cal < Judge_thr[0]) →{Table[0]}\r\n"
            //   + "\t      \t else if (Fincal_cal < Judge_thr[1]) →{Table[1]}\r\n"
            //   + "\t      \t else if (Fincal_cal < Judge_thr[2]) →{Table[2]}\r\n"
            //   + "\t      \t else if (Fincal_cal < Judge_thr[3]) →{Table[3]}\r\n"
            //   + "\t      \t else if (Fincal_cal < Judge_thr[4]) →{Table[4]}\r\n"
            //   + "\t      \t else →{Table[5]}\r\n"
            //   + "\t      \t 更多详见《调试工具用户手册》。\r\n";
            string str = "";


            XChip_OptimizationTips ot = new XChip_OptimizationTips("AE调优Tips", str);
            ot.StartPosition = FormStartPosition.CenterScreen;            
            //ot.ShowDialog();
            ot.Show();

        }

        private void chk_LFC_en_CheckedChanged(object sender, EventArgs e)
        {            
            if (LFC_Ctrl_En.CheckState == CheckState.Checked)
            {
                SetAeRegister("LFC_Ctrl_En", (ulong)0x01);
                insert_frame_num_max.Enabled = nAddExpLine.Enabled = true;
            }
            else
            {
                SetAeRegister("LFC_Ctrl_En", (ulong)0x00);
                insert_frame_num_max.Enabled = nAddExpLine.Enabled = false;
            }
        }

        private void txt_nAddExpLine_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter && !string.IsNullOrEmpty(nAddExpLine.Text))
            {                
                var nHigh = Convert.ToUInt32(nAddExpLine.Text, 16) >> 8;
                var nLow = Convert.ToUInt32(nAddExpLine.Text, 16) & 0x00ff;
                SetAeRegister("nAddExpLine_h", (ulong)nHigh);
                SetAeRegister("nAddExpLine_l", (ulong)nLow);
            }
        }

        private void txt_N_Table_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                var txtedit = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txtedit.Text)) return;
                int i = Convert.ToInt16(txtedit.Tag);
                SetAeRegister($"AE_TARGET_JUDGE_TAB{2 * i}_Night", (ulong)(Convert.ToInt32(txtedit.Text, 16) >> 8));
                SetAeRegister($"AE_TARGET_JUDGE_TAB{2 * i + 1 }_Night", (ulong)(Convert.ToInt32(txtedit.Text, 16) & 0x00ff));
            }           
        }

        private void txt_N_Judge_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                var txtedit = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txtedit.Text)) return;
                int i = Convert.ToInt16(txtedit.Tag);
                SetAeRegister($"AE_TARGET_JUDGE_THR{4 * i}_Night", (ulong)(Convert.ToInt32(txtedit.Text, 16) >> 24));
                SetAeRegister($"AE_TARGET_JUDGE_THR{4 * i + 1}_Night", (ulong)(Convert.ToInt32(txtedit.Text, 16) >> 16 & 0xff));
                SetAeRegister($"AE_TARGET_JUDGE_THR{4 * i + 2}_Night", (ulong)(Convert.ToInt32(txtedit.Text, 16) >> 8 & 0xff));
                SetAeRegister($"AE_TARGET_JUDGE_THR{4 * i + 3}_Night", (ulong)(Convert.ToInt32(txtedit.Text, 16) & 0xff));              
            }
        }

        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            SetAeRegister("Refresh", 0x01);//SerialSend.WriteRegister( | 0x005a, 0x01, sPortTemp);
        }

        private void chk_Mono_mode_CheckedChanged(object sender, EventArgs e)
        {
            //DayMonoSwitchLable(Mono_mode_en.Checked);
            //if (isRead) return;           
            //var readVal = GetAERegister("Mono_mode_en");
            //SetAeRegister("Mono_mode_en", (ulong)(Mono_mode_en.Checked ? (readVal | 0x80) : (readVal & 0x7f)));
            //if (isRead) return;
            //if (XChipAe.Exists(c => c.NameType.ToLower() == ($"Mono_mode_en").ToLower()))
            //{
            //    var model = XChipAe.Find(c => c.NameType.ToLower() == ($"Mono_mode_en").ToLower());
            //    var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
            //    var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), Mono_mode_en.Checked);
            //    SetAeRegister(model, Val);
            //}
        }

        private void DayMonoSwitchLable(bool isMono)
        {
            if (isMono)
            {
                txt_Judge_thr_0.Text = txt_Judge_thr_0.Text.Replace("normal", "mono");
                txt_Judge_thr_1.Text = txt_Judge_thr_1.Text.Replace("normal", "mono");
                txt_Judge_thr_2.Text = txt_Judge_thr_2.Text.Replace("normal", "mono");
                txt_Judge_thr_3.Text = txt_Judge_thr_3.Text.Replace("normal", "mono");
                txt_Judge_thr_4.Text = txt_Judge_thr_4.Text.Replace("normal", "mono");
                
                lbl_table_0.Text = lbl_table_0.Text.Replace("normal", "mono");
                lbl_table_1.Text = lbl_table_1.Text.Replace("normal", "mono");
                lbl_table_2.Text = lbl_table_2.Text.Replace("normal", "mono");
                lbl_table_3.Text = lbl_table_3.Text.Replace("normal", "mono");
                lbl_table_4.Text = lbl_table_4.Text.Replace("normal", "mono");
                lbl_table_5.Text = lbl_table_5.Text.Replace("normal", "mono");
            }
            else
            {
                txt_Judge_thr_0.Text = txt_Judge_thr_0.Text.Replace("mono", "normal");
                txt_Judge_thr_1.Text = txt_Judge_thr_1.Text.Replace("mono", "normal");
                txt_Judge_thr_2.Text = txt_Judge_thr_2.Text.Replace("mono", "normal");
                txt_Judge_thr_3.Text = txt_Judge_thr_3.Text.Replace("mono", "normal");
                txt_Judge_thr_4.Text = txt_Judge_thr_4.Text.Replace("mono", "normal");

                lbl_table_0.Text = lbl_table_0.Text.Replace("mono", "normal");
                lbl_table_1.Text = lbl_table_1.Text.Replace("mono", "normal");
                lbl_table_2.Text = lbl_table_2.Text.Replace("mono", "normal");
                lbl_table_3.Text = lbl_table_3.Text.Replace("mono", "normal");
                lbl_table_4.Text = lbl_table_4.Text.Replace("mono", "normal");
                lbl_table_5.Text = lbl_table_5.Text.Replace("mono", "normal");
            }
        }

        private void insert_frame_num_max_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && !string.IsNullOrEmpty(insert_frame_num_max.Text))
            {
                var nHigh = Convert.ToUInt32(insert_frame_num_max.Text, 16) ;
                SetAeRegister("insert_frame_num_max", (ulong)nHigh);
            }
        }

        private void start_blk_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && !string.IsNullOrEmpty(start_blk.Text))
            {
                var nHigh = Convert.ToUInt32(start_blk.Text, 16);
                SetAeRegister("start_blk", (ulong)nHigh);

                switch (AE_radiomode.SelectedIndex)
                {
                    case 0:
                        {

                            SetAeRegister("start_blk_day", (ulong)nHigh);

                            //SerialSend.WriteRegister(, (ulong)(nHigh & 0xff), sPortTemp);
                        }
                        break;
                    case 1:
                        {
                            SetAeRegister("start_blk_night", (ulong)nHigh);
                            //SerialSend.WriteRegister(0x2000145a, (ulong)(nHigh & 0xff), sPortTemp);
                            
                            //SerialSend.WriteRegister(, (ulong)(nHigh & 0xff), sPortTemp);
                        }
                        break;
                    case 2:
                        {
                            SetAeRegister("start_blk_Midnight", (ulong)nHigh);
                            //SerialSend.WriteRegister(0x2000145c, (ulong)(nHigh & 0xff), sPortTemp);
                            
                           
                        }
                        break;
                    default:
                        break;
                }
                //SerialSend.WriteRegister(0x20000240, (ulong)(nHigh & 0xff), sPortTemp);

            }
        }

        private void blk_num_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && !string.IsNullOrEmpty(blk_num.Text))
            {
                var nHigh = Convert.ToUInt32(blk_num.Text, 16);
                SetAeRegister("blk_num", (ulong)nHigh);
                switch (AE_radiomode.SelectedIndex)
                {
                    case 0:
                        {
                            //备注  数据库里面没有  暂时放地址
                            SetAeRegister("blk_num_day", (ulong)nHigh);
                            //SerialSend.WriteRegister(0x20001459, (ulong)(nHigh & 0xff), sPortTemp);
                            
                            //SerialSend.WriteRegister(, (ulong)(nHigh & 0xff), sPortTemp);
                        }
                        break;
                    case 1:
                        {
                            SetAeRegister("blk_num_night", (ulong)nHigh);
                            //SerialSend.WriteRegister(0x2000145b, (ulong)(nHigh & 0xff), sPortTemp);
                            
                            //SerialSend.WriteRegister(, (ulong)(nHigh & 0xff), sPortTemp);
                        }
                        break;
                    case 2:
                        {
                            SetAeRegister("blk_num_Midnight", (ulong)nHigh);
                            //SerialSend.WriteRegister(0x2000145d, (ulong)(nHigh & 0xff), sPortTemp);
                            
                        }
                        break;
                    default:
                        break;
                }
                //SerialSend.WriteRegister(0x20000241, (ulong)(nHigh & 0xff), sPortTemp);
            }
        }

        private void AE_WEIGHT1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //AE_WEIGHT1_Leave(sender, null);
            }
        }

        private void start_blk_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            start_blk_KeyDown(sender, keyEnter);
        }

        private void blk_num_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            blk_num_KeyDown(sender, keyEnter);
        }

        private void txt_nAddExpLine_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            txt_nAddExpLine_KeyDown(sender, keyEnter);
        }

        private void insert_frame_num_max_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            insert_frame_num_max_KeyDown(sender, keyEnter);
        }

        private void Newmode_en_CheckedChanged(object sender, EventArgs e)
        {
      
            if (Newmode_en.Checked)
            {
                start_blk.Enabled = true;
                blk_num.Enabled = true;
                //AE_WEIGHTENABLE_EN.CheckState = CheckState.Unchecked;
                SetAeRegister("Newmode_en", 0x1);
                SetAeRegister("avg_calc_en", 0x1);
            } else {
                start_blk.Enabled = false;
                blk_num.Enabled = false;
                //AE_WEIGHTENABLE_EN.CheckState = CheckState.Checked;
                SetAeRegister("Newmode_en", 0x0);
                SetAeRegister("avg_calc_en", 0x0);
            }
               
            
        }

        private void simpleButton4_Click_1(object sender, EventArgs e)
        {
            if (IsUsbOpen || m_open)
            {

            }
            else
            {
                if (!sPortTemp.IsOpen)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                    return;
                }
            }
            //SerialSend.isDebug = false;
            SaveFileDialog sfd = new SaveFileDialog();
            //设置文件类型 
            sfd.Filter = "配置文件（*.txt）|*.txt";

            //设置默认文件类型显示顺序 
            sfd.FilterIndex = 1;

            //保存对话框是否记忆上次打开的目录 
            sfd.RestoreDirectory = true;
            sfd.FileName = "IQ_Setting_AE_" + DateTime.Now.ToString("yyyyMMddHHmmss");
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                var Pe_dal = new parameter_exportManager();
                if (null != Pe_dal)
                {
                    WaitBefore.Start("参数导出", "正在加载中", 60);
                    //var tempList = Pe_dal.GetList(c => c.Module == "AWB");
                    var tempList = Pe_dal.GetListToExport().Where(c => c.Module == "Area Weight" || 
                    c.Module == "Attention" || 
                    c.Module == "AE Base0" || 
                    c.Module == "AE speed" || 
                    c.Module == "AE speed table" ||
                    c.Module.Contains("avg_weight 0x8014005b")||
                    c.Module == "Target_table_day"||
                    c.Module == "Target_table_night"||
                    c.Module == "Target_judge_thr_day"||
                    c.Module == "Target_judge_thr_night"||
                    c.Module.Contains("avg_size 0x80301f00")||
                    c.Module == "AE window").OrderBy(c => c.SortNum).ToList();
                    #region 判断是否需要用白天的数据
                    var Pe_patch_dal = new parameter_export_patchManager();
                    var patchList = Pe_patch_dal.GetList();
                    var conditionList = tempList.Select(c => c.ConditionAddr).Distinct();
                    foreach (var item in conditionList)
                    {


                        if (GetAERegister(item) == 0)
                        {
                            patchList.RemoveAll(c => c.ConditionAddr == item);
                        }
                    }
                    #endregion
                    var sb = XChipHelp.ReadParameterExportExpand(tempList, "80", patchList, sPortTemp);
                    System.IO.File.WriteAllText(sfd.FileName, sb.ToString());

                }
                WaitBefore.Close();
                DevExpress.XtraEditors.XtraMessageBox.Show("参数导出完成");
            }
        }

       

        private void simpleButton5_MouseEnter(object sender, EventArgs e)
        {
              isWrite = true;
        }

        private void simpleButton5_MouseLeave(object sender, EventArgs e)
        {
              isWrite = false;
        }

        private void speed_mode_man_en_CheckedChanged(object sender, EventArgs e)
        {
            var chk = sender as CheckEdit;
            if (isRead) return;
            if (XChipAe.Exists(c => c.NameType.ToLower() == (chk.Name).ToLower()))
            {
                var model = XChipAe.Find(c => c.NameType.ToLower() == (chk.Name).ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), chk.Checked);
                SetAeRegister(model, Val);
            }
            if (chk.Checked)
            {
                SetAeRegister("speed_mode_man", 0x01);
            }
        }

        private void AE_SpeedModeradio_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isRead) return;
            var aeradio = sender as DevExpress.XtraEditors.RadioGroup;
            if (aeradio.EditValue.ToString() == "1")
            {
                SetAeRegister("speed_mode_man", 0x01);
            }
            else if (aeradio.EditValue.ToString() == "2")
            {
                SetAeRegister("speed_mode_man", 0x02);
            }
            else if (aeradio.EditValue.ToString() == "3")
            {
                SetAeRegister("speed_mode_man", 0x03);
            }
        }

        private void slow_b2d_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
             if (null != sPortTemp || !IsUsbOpen || !m_open)
             {
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;

                //if (XChipLenc.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                if (XChipAe.Exists(c => c.NameType.ToLower() == $"{txt.Name.ToLower()}"))
                {
                    var model = XChipAe.Find(c => c.NameType.ToLower() == $"{txt.Name.ToLower()}");
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetAeRegister(model, Val);
                }
            }
        }

        private void slow_b2d_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                slow_b2d_Leave(sender, null);
            }
        }

        private void AE_Flicker_50Hz_Leave(object sender, EventArgs e)
        {
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;

            if (XChipAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
            {
                var model = XChipAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetAeRegister(model, Val);
            }
            if (XChipAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
            {
                var model = XChipAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetAeRegister(model, Val);
            }
        }

        private void AE_Flicker_50Hz_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                AE_Flicker_50Hz_Leave(sender, null);
            }
        }

        private void Mode_switch_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (Mode_switch.EditValue.ToString() == "1")
            //{
            //    //Table[0]_normal
            //    lbl_table_0.Text = lbl_table_0.Text.Replace("normal", "").Replace("Gray", "") + "Color";
            //    lbl_table_1.Text = lbl_table_1.Text.Replace("normal", "").Replace("Gray", "") + "Color";
            //    lbl_table_2.Text = lbl_table_2.Text.Replace("normal", "").Replace("Gray", "") + "Color";
            //    lbl_table_3.Text = lbl_table_3.Text.Replace("normal", "").Replace("Gray", "") + "Color";
            //    lbl_table_4.Text = lbl_table_4.Text.Replace("normal", "").Replace("Gray", "") + "Color";
            //    lbl_table_5.Text = lbl_table_5.Text.Replace("normal", "").Replace("Gray", "") + "Color";

            //    txt_Judge_thr_0.Text = txt_Judge_thr_0.Text.Replace("normal", "").Replace("Gray", "") + "Color";
            //    txt_Judge_thr_1.Text = txt_Judge_thr_1.Text.Replace("normal", "").Replace("Gray", "") + "Color";
            //    txt_Judge_thr_2.Text = txt_Judge_thr_2.Text.Replace("normal", "").Replace("Gray", "") + "Color";
            //    txt_Judge_thr_3.Text = txt_Judge_thr_3.Text.Replace("normal", "").Replace("Gray", "") + "Color";
            //    txt_Judge_thr_4.Text = txt_Judge_thr_4.Text.Replace("normal", "").Replace("Gray", "") + "Color";
            //}
            //else
            //{
            //    lbl_table_0.Text = lbl_table_0.Text.Replace("normal", "").Replace("Color", "") + "Gray";
            //    lbl_table_1.Text = lbl_table_1.Text.Replace("normal", "").Replace("Color", "") + "Gray";
            //    lbl_table_2.Text = lbl_table_2.Text.Replace("normal", "").Replace("Color", "") + "Gray";
            //    lbl_table_3.Text = lbl_table_3.Text.Replace("normal", "").Replace("Color", "") + "Gray";
            //    lbl_table_4.Text = lbl_table_4.Text.Replace("normal", "").Replace("Color", "") + "Gray";
            //    lbl_table_5.Text = lbl_table_5.Text.Replace("normal", "").Replace("Color", "") + "Gray";

            //    txt_Judge_thr_0.Text = txt_Judge_thr_0.Text.Replace("normal", "").Replace("Color", "") + "Gray";
            //    txt_Judge_thr_1.Text = txt_Judge_thr_1.Text.Replace("normal", "").Replace("Color", "") + "Gray";
            //    txt_Judge_thr_2.Text = txt_Judge_thr_2.Text.Replace("normal", "").Replace("Color", "") + "Gray";
            //    txt_Judge_thr_3.Text = txt_Judge_thr_3.Text.Replace("normal", "").Replace("Color", "") + "Gray";
            //    txt_Judge_thr_4.Text = txt_Judge_thr_4.Text.Replace("normal", "").Replace("Color", "") + "Gray";
            //}
        }
        private void EnShowHide()
        {
            if (checkedListBoxControl2.Items[0].CheckState == CheckState.Checked)//50hz
            {
                AE_Flicker_50Hz.Enabled = true;
                AE_Flicker_60Hz.Enabled = false;
            }
            if (checkedListBoxControl2.Items[1].CheckState == CheckState.Checked)//60hz
            {
                AE_Flicker_50Hz.Enabled = false;
                AE_Flicker_60Hz.Enabled = true;
            }
            if ((checkedListBoxControl2.Items[1].CheckState == CheckState.Unchecked) && (checkedListBoxControl2.Items[0].CheckState == CheckState.Unchecked))//均不勾选
            {
                AE_Flicker_50Hz.Enabled = false;
                AE_Flicker_60Hz.Enabled = false;
            }

            slow_b2d.Enabled = speed_mode_man_en.Checked;
            slow_d2b.Enabled = speed_mode_man_en.Checked;

            start_blk.Enabled = Newmode_en.Checked;
            blk_num.Enabled = Newmode_en.Checked;
        }

        private void Target_switch_mode_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isRead) return;
            var aeradio = sender as DevExpress.XtraEditors.RadioGroup;
            if (aeradio.EditValue.ToString() == "1")
            {
                SetAeRegister("speed_mode_man", 0x00);
                TargetSwitchEnShowHide(false);
            }
            else if (aeradio.EditValue.ToString() == "2")
            {
                SetAeRegister("speed_mode_man", 0x01);
                TargetSwitchEnShowHide(true);
            }
            else if (aeradio.EditValue.ToString() == "3")
            {
                SetAeRegister("speed_mode_man", 0x02);
                TargetSwitchEnShowHide(true);
            }
        }

        private void speed_mode_man_en_CheckedChanged_1(object sender, EventArgs e)
        {
            var chk = sender as CheckEdit;
            if (isRead) return;
            if (XChipAe.Exists(c => c.NameType.ToLower() == (chk.Name).ToLower()))
            {
                var model = XChipAe.Find(c => c.NameType.ToLower() == (chk.Name).ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), chk.Checked);
                SetAeRegister(model, Val);
            }
            if (chk.Checked)
            {
                SetAeRegister("speed_mode_man", 0x01);
            }
            EnShowHide();
        }

        private void slow_b2d_Leave_1(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (null != sPortTemp || !IsUsbOpen || !m_open)
            {
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;

                //if (XChipLenc.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                if (XChipAe.Exists(c => c.NameType.ToLower() == $"{txt.Name.ToLower()}"))
                {
                    var model = XChipAe.Find(c => c.NameType.ToLower() == $"{txt.Name.ToLower()}");
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetAeRegister(model, Val);
                }
            }
        }

        private void slow_b2d_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                slow_b2d_Leave(sender, null);
            }
        }

        private void COMBO_GAMMAPRE_EditValueChanged(object sender, EventArgs e)
        {
            if (isRead) return;
            if (COMBO_GAMMAPRE.EditValue.ToString() == "0")
            {
                SetAeRegister("Target_switch", 0x00);
                TargetSwitchEnShowHide(false);
            }
            else if (COMBO_GAMMAPRE.EditValue.ToString() == "1")
            {
                SetAeRegister("Target_switch", 0x01);
                TargetSwitchEnShowHide(true);
            }
            else if (COMBO_GAMMAPRE.EditValue.ToString() == "2")
            {
                SetAeRegister("Target_switch", 0x02);
                TargetSwitchEnShowHide(true);
            }
        }
    }
} 